Skip to content

Commit

Permalink
improve checks test coverage (#319)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #319

Add additional tests to improve test coverage and prevent from breaking an API

Reviewed By: t3lurid3

Differential Revision: D52664462

fbshipit-source-id: b1a99925eb8f773092c9a5fcde5530f19a26f71a
  • Loading branch information
leoleovich authored and facebook-github-bot committed Jan 10, 2024
1 parent 64869d4 commit d86adf5
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 4 deletions.
11 changes: 11 additions & 0 deletions calnex/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ func TestChannel(t *testing.T) {
}
}

func TestChannels(t *testing.T) {
cs := Channels{}
require.Equal(t, "channel", cs.Type())

err := cs.Set("1")
require.NoError(t, err)
err = cs.Set("A")
require.NoError(t, err)
require.Equal(t, "1, A", cs.String())
}

func TestProbe(t *testing.T) {
legitProbeNamesToProbe := map[string]Probe{
"ntp": ProbeNTP,
Expand Down
163 changes: 159 additions & 4 deletions calnex/verify/checks/checks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ limitations under the License.
package checks

import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"

"github.com/facebook/time/calnex/api"
"github.com/stretchr/testify/require"
)

Expand All @@ -27,11 +33,71 @@ func TestGNSS(t *testing.T) {
c := GNSS{Remediation: r}
require.Equal(t, "GNSS", c.Name())

sampleResp := "{\"antennaStatus\":\"OK\",\"locked\":true,\"lockedSatellites\":9,\"surveyComplete\":true,\"surveyPercentComplete\":100}"
ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter,
r *http.Request) {
fmt.Fprintln(w, sampleResp)
}))
defer ts.Close()

parsed, _ := url.Parse(ts.URL)
calnexAPI := api.NewAPI(parsed.Host, true, time.Second)
calnexAPI.Client = ts.Client()

err := c.Run(parsed.Host, true)
require.NoError(t, err)
}

func TestGNSSNoSatellites(t *testing.T) {
r := GNSSRemediation{}
c := GNSS{Remediation: r}
require.Equal(t, "GNSS", c.Name())

sampleResp := "{\"antennaStatus\":\"OK\",\"locked\":true,\"lockedSatellites\":2,\"surveyComplete\":true,\"surveyPercentComplete\":100}"
ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter,
r *http.Request) {
fmt.Fprintln(w, sampleResp)
}))
defer ts.Close()

parsed, _ := url.Parse(ts.URL)
calnexAPI := api.NewAPI(parsed.Host, true, time.Second)
calnexAPI.Client = ts.Client()

err := c.Run(parsed.Host, true)
require.ErrorContains(t, err, "gnss: not enough satellites")
}

func TestGNSSNoAntenna(t *testing.T) {
r := GNSSRemediation{}
c := GNSS{Remediation: r}
require.Equal(t, "GNSS", c.Name())

sampleResp := "{\"antennaStatus\":\"BAD\",\"locked\":true,\"lockedSatellites\":9,\"surveyComplete\":true,\"surveyPercentComplete\":100}"
ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter,
r *http.Request) {
fmt.Fprintln(w, sampleResp)
}))
defer ts.Close()

parsed, _ := url.Parse(ts.URL)
calnexAPI := api.NewAPI(parsed.Host, true, time.Second)
calnexAPI.Client = ts.Client()

err := c.Run(parsed.Host, true)
require.ErrorContains(t, err, "gnss: antenna status is: BAD")
}

func TestGNSSError(t *testing.T) {
r := GNSSRemediation{}
c := GNSS{Remediation: r}
require.Equal(t, "GNSS", c.Name())

err := c.Run("1.2.3.4", false)
require.Error(t, err)

want, _ := r.Remediate()
got, err := c.Remediation.Remediate()
got, err := c.Remediate()
require.NoError(t, err)
require.Equal(t, want, got)
}
Expand All @@ -41,11 +107,31 @@ func TestHTTP(t *testing.T) {
c := HTTP{Remediation: r}
require.Equal(t, "HTTP", c.Name())

sampleResp := "{\"channelLinksReady\":true,\"ipAddressReady\":true,\"measurementActive\":true,\"measurementReady\":true,\"modulesReady\":true,\"referenceReady\":true}"
ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter,
r *http.Request) {
fmt.Fprintln(w, sampleResp)
}))
defer ts.Close()

parsed, _ := url.Parse(ts.URL)
calnexAPI := api.NewAPI(parsed.Host, true, time.Second)
calnexAPI.Client = ts.Client()

err := c.Run(parsed.Host, true)
require.NoError(t, err)
}

func TestHTTPError(t *testing.T) {
r := HTTPRemediation{}
c := HTTP{Remediation: r}
require.Equal(t, "HTTP", c.Name())

err := c.Run("1.2.3.4", false)
require.Error(t, err)

want, _ := r.Remediate()
got, err := c.Remediation.Remediate()
got, err := c.Remediate()
require.NoError(t, err)
require.Equal(t, want, got)
}
Expand All @@ -55,11 +141,20 @@ func TestPing(t *testing.T) {
c := Ping{Remediation: r}
require.Equal(t, "Ping", c.Name())

err := c.Run("::1", false)
require.NoError(t, err)
}

func TestPingError(t *testing.T) {
r := PingRemediation{}
c := Ping{Remediation: r}
require.Equal(t, "Ping", c.Name())

err := c.Run("1.2.3.4", false)
require.Error(t, err)

want, _ := r.Remediate()
got, err := c.Remediation.Remediate()
got, err := c.Remediate()
require.NoError(t, err)
require.Equal(t, want, got)
}
Expand All @@ -69,11 +164,71 @@ func TestPSU(t *testing.T) {
c := PSU{Remediation: r}
require.Equal(t, "PSU", c.Name())

sampleResp := "{\"power_supply_good\":true,\"supplies\":[{\"comms_good\":true,\"name\":\"PSU_module_A\",\"status_good\":true},{\"comms_good\":true,\"name\":\"PSU_module_B\",\"status_good\":true}]}"
ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter,
r *http.Request) {
fmt.Fprintln(w, sampleResp)
}))
defer ts.Close()

parsed, _ := url.Parse(ts.URL)
calnexAPI := api.NewAPI(parsed.Host, true, time.Second)
calnexAPI.Client = ts.Client()

err := c.Run(parsed.Host, true)
require.NoError(t, err)
}

func TestPSUSingleBad(t *testing.T) {
r := PSURemediation{}
c := PSU{Remediation: r}
require.Equal(t, "PSU", c.Name())

sampleResp := "{\"power_supply_good\":false,\"supplies\":[{\"comms_good\":true,\"name\":\"PSU_module_A\",\"status_good\":true},{\"comms_good\":true,\"name\":\"PSU_module_B\",\"status_good\":false}]}"
ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter,
r *http.Request) {
fmt.Fprintln(w, sampleResp)
}))
defer ts.Close()

parsed, _ := url.Parse(ts.URL)
calnexAPI := api.NewAPI(parsed.Host, true, time.Second)
calnexAPI.Client = ts.Client()

err := c.Run(parsed.Host, true)
require.ErrorContains(t, err, "psu: failed power supply #1: PSU_module_B")
}

func TestPSUBBad(t *testing.T) {
r := PSURemediation{}
c := PSU{Remediation: r}
require.Equal(t, "PSU", c.Name())

sampleResp := "{\"power_supply_good\":false}"
ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter,
r *http.Request) {
fmt.Fprintln(w, sampleResp)
}))
defer ts.Close()

parsed, _ := url.Parse(ts.URL)
calnexAPI := api.NewAPI(parsed.Host, true, time.Second)
calnexAPI.Client = ts.Client()

err := c.Run(parsed.Host, true)
require.ErrorContains(t, err, "psu: failed power supply")
}

func TestPSUError(t *testing.T) {
r := PSURemediation{}
c := PSU{Remediation: r}
require.Equal(t, "PSU", c.Name())

err := c.Run("1.2.3.4", false)
require.Error(t, err)

want, _ := r.Remediate()
got, err := c.Remediation.Remediate()
got, err := c.Remediate()
require.NoError(t, err)
require.Equal(t, want, got)
}

0 comments on commit d86adf5

Please sign in to comment.