Skip to content

Commit

Permalink
refactor: Integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ctlong committed Mar 21, 2024
1 parent 4e6312b commit db67213
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 57 deletions.
29 changes: 9 additions & 20 deletions main_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,21 @@ import (
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gexec"

"os/exec"
"testing"
)

func TestUptimer(t *testing.T) {
func TestIntegration(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Main Suite")
RunSpecs(t, "Integration Suite")
}

var commandPath string
var uptimerPath string

var _ = BeforeSuite(func() {
executablePath, err := Build("github.com/cloudfoundry/uptimer")
var _ = SynchronizedBeforeSuite(func() []byte {
path, err := Build("github.com/cloudfoundry/uptimer")
Expect(err).NotTo(HaveOccurred())
commandPath = string(executablePath)
DeferCleanup(CleanupBuildArtifacts)
return []byte(path)
}, func(data []byte) {
uptimerPath = string(data)
})

var _ = AfterSuite(func() {
CleanupBuildArtifacts()
})

func runCommand(args ...string) *Session {
cmd := exec.Command(commandPath, args...)
session, err := Start(cmd, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
<-session.Exited

return session
}
129 changes: 92 additions & 37 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,48 +1,103 @@
package main_test

import (
"encoding/json"
"os"
"os/exec"

"github.com/cloudfoundry/uptimer/config"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gbytes"
. "github.com/onsi/gomega/gexec"
)

var _ = Describe("Main", func() {
It("Prints error when when run_app_syslog_availability and tcp_domain and available_port are not provided", func() {
configFile, err := os.CreateTemp("", "config")
Expect(err).ToNot(HaveOccurred())

defer os.Remove(configFile.Name())

configJson := `{
"while": [
{
"command": "sleep",
"command_args": ["5"]
}
],
"cf": {
"api": "api.my-cf.com",
"app_domain": "my-cf.com",
"admin_user": "admin",
"admin_password": "PASS"
},
"optional_tests": {
"run_app_syslog_availability": true
},
"allowed_failures": {
"app_pushability": 2,
"http_availability": 5,
"recent_logs": 2,
"streaming_logs": 2,
"app_syslog_availability": 2
}
}`
_, err = configFile.WriteString(configJson)
Expect(err).ToNot(HaveOccurred())

session := runCommand("-configFile", configFile.Name())

Expect(string(session.Out.Contents())).To(ContainSubstring("`cf.tcp_domain` and `cf.available_port` must be set in order to run App Syslog Availability tests."))
var _ = Describe("uptimer", func() {
var (
cfg *config.Config
session *Session
)

BeforeEach(func() {
cfg = &config.Config{
While: []*config.Command{
{
Command: "sleep",
CommandArgs: []string{"5"},
},
},
CF: &config.Cf{
API: "api.my-cf.com",
AppDomain: "my-cf.com",
AdminUser: "admin",
AdminPassword: "pass",
},
AllowedFailures: config.AllowedFailures{
AppPushability: 2,
HttpAvailability: 5,
RecentLogs: 2,
StreamingLogs: 2,
},
}
})

JustBeforeEach(func() {
tmpDir := GinkgoT().TempDir()
f, err := os.Create(tmpDir + "/config.json")
Expect(err).NotTo(HaveOccurred())
defer f.Close()
b, err := json.Marshal(cfg)
Expect(err).NotTo(HaveOccurred())
_, err = f.Write(b)
Expect(err).NotTo(HaveOccurred())
cmd := exec.Command(uptimerPath, "-configFile", f.Name())
session, err = Start(cmd, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
Eventually(session, "10s").Should(Exit())
})

Context("when configured to test app syslog availability", func() {
BeforeEach(func() {
cfg.OptionalTests.RunAppSyslogAvailability = true
cfg.AllowedFailures.AppSyslogAvailability = 2
})

Context("when tcp_domain and available_port are not provided", func() {
BeforeEach(func() {
cfg.CF.AvailablePort = 0
cfg.CF.TCPDomain = ""
})

It("exits with a error code of 1", func() {
Expect(session.ExitCode()).To(Equal(1))
})

It("prints an error", func() {
Expect(session.Out).To(Say("`cf.tcp_domain` and `cf.available_port` must be set in order to run App Syslog Availability tests."))
})
})
})

Context("when configured to test TCP availability", func() {
BeforeEach(func() {
cfg.OptionalTests.RunTcpAvailability = true
cfg.AllowedFailures.TCPAvailability = 2
})

Context("when tcp_domain and tcp_port are not provided", func() {
BeforeEach(func() {
cfg.CF.TCPDomain = ""
cfg.CF.TCPPort = 0
})

It("exits with a error code of 1", func() {
Expect(session.ExitCode()).To(Equal(1))
})

It("prints an error", func() {
Expect(session.Out).To(Say("`cf.tcp_domain` and `cf.tcp_port` must be set in order to run TCP Availability tests."))
})
})
})
})

0 comments on commit db67213

Please sign in to comment.