Skip to content

Commit

Permalink
cleaning up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jm96441n committed May 23, 2024
1 parent 2687db2 commit 679f448
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 41 deletions.
11 changes: 4 additions & 7 deletions dependency/consul_exported_services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,20 +137,17 @@ func TestListExportedServicesQuery_Fetch(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}

q := &ListExportedServicesQuery{
stopCh: make(chan struct{}),
partition: tc.partition,
}
q, err := NewListExportedServicesQuery(tc.partition)
require.NoError(t, err)

actual, _, err := q.Fetch(testClients, nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
require.NoError(t, err)

require.ElementsMatch(t, tc.expected, actual)

// need to clean up because we use a single shared consul instance
_, err = testClients.Consul().ConfigEntries().Delete(capi.ExportedServices, tc.exportedServices.Name, &capi.WriteOptions{Partition: tc.partition})
require.NoError(t, err)
})
}
}
28 changes: 13 additions & 15 deletions dependency/consul_partitions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,22 @@ func TestNewListPartitionsQuery(t *testing.T) {
}

func TestListPartitionsQuery_Fetch(t *testing.T) {
cases := []struct {
name string
exp []Partition
}{
expected := []*Partition{
{
Name: "default",
Description: "Builtin Default Partition",
},

{
"default",
[]Partition{{Name: "dc1", Description: "dc1"}},
Name: "foo",
Description: "",
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
d, err := NewListPartitionsQuery()
require.NoError(t, err)
d, err := NewListPartitionsQuery()
require.NoError(t, err)

act, _, err := d.Fetch(nil, nil)
require.NoError(t, err)
assert.Equal(t, tc.exp, act)
})
}
act, _, err := d.Fetch(testClients, nil)
require.NoError(t, err)
assert.Equal(t, expected, act)
}
33 changes: 15 additions & 18 deletions dependency/dependency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ func TestMain(m *testing.M) {
tb := &test.TestingTB{}
runTestConsul(tb)
clients := NewClientSet()

defer func() {
// Attempt to recover from a panic and stop the server. If we don't
// stop it, the panic will cause the server to remain running in
// the background. Here we catch the panic and the re-raise it.
// This doesn't do anything if we get panics in individual test cases
if r := recover(); r != nil {
testConsul.Stop()
testVault.Stop()
testNomad.Stop()
panic(r)
}
}()

if err := clients.CreateConsulClient(&CreateConsulClientInput{
Address: testConsul.HTTPAddr,
}); err != nil {
Expand Down Expand Up @@ -111,24 +125,7 @@ func TestMain(m *testing.M) {
Fatalf("failed to start Nomad: %v\n", err)
}

exitCh := make(chan int, 1)
func() {
defer func() {
// Attempt to recover from a panic and stop the server. If we don't
// stop it, the panic will cause the server to remain running in
// the background. Here we catch the panic and the re-raise it.
if r := recover(); r != nil {
testConsul.Stop()
testVault.Stop()
testNomad.Stop()
panic(r)
}
}()

exitCh <- m.Run()
}()

exit := <-exitCh
exit := m.Run()

tb.DoCleanup()
testConsul.Stop()
Expand Down
1 change: 0 additions & 1 deletion dependency/nomad_var_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ func TestNewNVGetQuery(t *testing.T) {
assert.Equal(t, tc.exp, act)
})
}
fmt.Println("done")
}

func TestNVGetQuery_Fetch(t *testing.T) {
Expand Down

0 comments on commit 679f448

Please sign in to comment.