|
| 1 | +package events |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "reflect" |
| 8 | + "testing" |
| 9 | + |
| 10 | + drycc "github.com/drycc/controller-sdk-go" |
| 11 | + "github.com/drycc/controller-sdk-go/api" |
| 12 | + "github.com/drycc/controller-sdk-go/pkg/time" |
| 13 | +) |
| 14 | + |
| 15 | +const podEventsFixture string = ` |
| 16 | +{ |
| 17 | + "count": 1, |
| 18 | + "next": null, |
| 19 | + "previous": null, |
| 20 | + "results": [ |
| 21 | + { |
| 22 | + "reason": "Scheduled", |
| 23 | + "message": "Successfully assigned example-go/example-go-web-6b44dbd6c8-h89cg to node1", |
| 24 | + "created": "2024-07-03T16:28:00" |
| 25 | + } |
| 26 | + ] |
| 27 | +}` |
| 28 | + |
| 29 | +const ptypeEventsFixture string = ` |
| 30 | +{ |
| 31 | + "count": 1, |
| 32 | + "next": null, |
| 33 | + "previous": null, |
| 34 | + "results": [ |
| 35 | + { |
| 36 | + "reason": "ScalingReplicaSet", |
| 37 | + "message": "Scaled up replica set example-go-web-6b44dbd6c8 to 2 from 1", |
| 38 | + "created": "2024-07-03T16:28:00" |
| 39 | + } |
| 40 | + ] |
| 41 | +}` |
| 42 | + |
| 43 | +type fakeHTTPServer struct{} |
| 44 | + |
| 45 | +func (fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) { |
| 46 | + res.Header().Add("DRYCC_API_VERSION", drycc.APIVersion) |
| 47 | + if req.URL.Path == "/v2/apps/example-go/events/" && req.Method == "GET" && req.URL.RawQuery == "ptype_name=example-go-web" { |
| 48 | + res.Write([]byte(ptypeEventsFixture)) |
| 49 | + return |
| 50 | + } |
| 51 | + if req.URL.Path == "/v2/apps/example-go/events/" && req.Method == "GET" && req.URL.RawQuery == "pod_name=example-go-web-6b44dbd6c8-h89cg" { |
| 52 | + res.Write([]byte(podEventsFixture)) |
| 53 | + return |
| 54 | + } |
| 55 | + |
| 56 | + fmt.Printf("Unrecognized URL %s\n", req.URL) |
| 57 | + res.WriteHeader(http.StatusNotFound) |
| 58 | + res.Write(nil) |
| 59 | +} |
| 60 | + |
| 61 | +func TestEvents(t *testing.T) { |
| 62 | + t.Parallel() |
| 63 | + |
| 64 | + created := time.Time{} |
| 65 | + created.UnmarshalText([]byte("2024-07-03T16:28:00")) |
| 66 | + podExpected := api.AppEvents{ |
| 67 | + { |
| 68 | + Reason: "Scheduled", |
| 69 | + Message: "Successfully assigned example-go/example-go-web-6b44dbd6c8-h89cg to node1", |
| 70 | + Created: created, |
| 71 | + }, |
| 72 | + } |
| 73 | + ptypeExpected := api.AppEvents{ |
| 74 | + { |
| 75 | + Reason: "ScalingReplicaSet", |
| 76 | + Message: "Scaled up replica set example-go-web-6b44dbd6c8 to 2 from 1", |
| 77 | + Created: created, |
| 78 | + }, |
| 79 | + } |
| 80 | + |
| 81 | + handler := fakeHTTPServer{} |
| 82 | + server := httptest.NewServer(handler) |
| 83 | + defer server.Close() |
| 84 | + |
| 85 | + drycc, err := drycc.New(false, server.URL, "abc") |
| 86 | + if err != nil { |
| 87 | + t.Fatal(err) |
| 88 | + } |
| 89 | + |
| 90 | + actual, _, err := ListPodEvents(drycc, "example-go", "example-go-web-6b44dbd6c8-h89cg", 100) |
| 91 | + |
| 92 | + if err != nil { |
| 93 | + t.Fatal(err) |
| 94 | + } |
| 95 | + |
| 96 | + if !reflect.DeepEqual(podExpected, actual) { |
| 97 | + t.Error(fmt.Errorf("Expected %v, Got %v", podExpected, actual)) |
| 98 | + } |
| 99 | + |
| 100 | + actual, _, err = ListPtypeEvents(drycc, "example-go", "web", 100) |
| 101 | + |
| 102 | + if err != nil { |
| 103 | + t.Fatal(err) |
| 104 | + } |
| 105 | + |
| 106 | + if !reflect.DeepEqual(ptypeExpected, actual) { |
| 107 | + t.Error(fmt.Errorf("Expected %v, Got %v", ptypeExpected, actual)) |
| 108 | + } |
| 109 | +} |
0 commit comments