|
| 1 | +package app |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "os" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + . "github.com/onsi/ginkgo" |
| 11 | + . "github.com/onsi/gomega" |
| 12 | + |
| 13 | + addonutils "open-cluster-management.io/addon-framework/pkg/utils" |
| 14 | +) |
| 15 | + |
| 16 | +func TestServer(t *testing.T) { |
| 17 | + RegisterFailHandler(Fail) |
| 18 | + RunSpecs(t, "Server Suite") |
| 19 | +} |
| 20 | + |
| 21 | +var tmpFileName string |
| 22 | + |
| 23 | +var _ = BeforeSuite(func() { |
| 24 | + tmpFile, err := os.CreateTemp("/tmp", "test") |
| 25 | + Expect(err).To(BeNil()) |
| 26 | + |
| 27 | + tmpFileName = tmpFile.Name() |
| 28 | + |
| 29 | + cc, err := addonutils.NewConfigChecker("test", tmpFileName) |
| 30 | + Expect(err).To(BeNil()) |
| 31 | + |
| 32 | + go ServeHealthProbes(context.Background().Done(), ":8000", cc.Check) |
| 33 | +}) |
| 34 | + |
| 35 | +var _ = Describe("Config changed", func() { |
| 36 | + BeforeEach(func() { |
| 37 | + // pre check |
| 38 | + resp, err := http.Get("http://localhost:8000/healthz") |
| 39 | + Expect(err).To(BeNil()) |
| 40 | + Expect(resp.StatusCode).To(Equal(http.StatusOK)) |
| 41 | + |
| 42 | + // change the config |
| 43 | + err = os.WriteFile(tmpFileName, []byte("changed content"), os.ModeAppend) |
| 44 | + Expect(err).To(BeNil()) |
| 45 | + }) |
| 46 | + It("should return false", func() { |
| 47 | + // check |
| 48 | + resp, err := http.Get("http://localhost:8000/healthz") |
| 49 | + Expect(err).To(BeNil()) |
| 50 | + Expect(resp.StatusCode).To(Equal(http.StatusInternalServerError)) |
| 51 | + |
| 52 | + time.Sleep(3 * time.Second) |
| 53 | + // check again |
| 54 | + resp, err = http.Get("http://localhost:8000/healthz") |
| 55 | + Expect(err).To(BeNil()) |
| 56 | + Expect(resp.StatusCode).To(Equal(http.StatusInternalServerError)) |
| 57 | + }) |
| 58 | +}) |
0 commit comments