open-nomad/internal/testing/apitests/structsync_test.go
Jeff Mitchell 13dab7dd24
Divest api/ package of deps elsewhere in the nomad repo. (#5488)
* Divest api/ package of deps elsewhere in the nomad repo.

This will allow making api/ a module without then pulling in the
external repo, leading to a package name conflict.

This required some migration of tests to an apitests/ folder (can be
moved anywhere as it has no deps on it). It also required some
duplication of code, notably some test helpers from api/ -> apitests/
and part (but not all) of testutil/ -> api/testutil/.

Once there's more separation and an e.g. sdk/ folder those can be
removed in favor of a dep on the sdk/ folder, provided the sdk/ folder
doesn't depend on api/ or /.

* Also remove consul dep from api/ package

* Fix stupid linters

* Some restructuring
2019-03-29 14:47:40 -04:00

103 lines
2.5 KiB
Go

package apitests
import (
"encoding/json"
"testing"
"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/stretchr/testify/require"
)
// Tests that api and struct values are equivalent
//
// Given that vendoring libraries prune tests by default, test dependencies
// aren't leaked to clients of the package - so it should be safe to add
// such dependency without affecting api clients.
func TestDefaultResourcesAreInSync(t *testing.T) {
apiR := api.DefaultResources()
structsR := structs.DefaultResources()
require.EqualValues(t, *structsR, toStructsResource(t, apiR))
// match after canonicalization
apiR.Canonicalize()
structsR.Canonicalize()
require.EqualValues(t, *structsR, toStructsResource(t, apiR))
}
func TestMinResourcesAreInSync(t *testing.T) {
apiR := api.MinResources()
structsR := structs.MinResources()
require.EqualValues(t, *structsR, toStructsResource(t, apiR))
// match after canonicalization
apiR.Canonicalize()
structsR.Canonicalize()
require.EqualValues(t, *structsR, toStructsResource(t, apiR))
}
func TestNewDefaultRescheulePolicyInSync(t *testing.T) {
cases := []struct {
typ string
expected structs.ReschedulePolicy
}{
{"service", structs.DefaultServiceJobReschedulePolicy},
{"batch", structs.DefaultBatchJobReschedulePolicy},
{"system", structs.ReschedulePolicy{}},
}
for _, c := range cases {
t.Run(c.typ, func(t *testing.T) {
apiP := api.NewDefaultReschedulePolicy(c.typ)
var found structs.ReschedulePolicy
toStructs(t, &found, apiP)
require.EqualValues(t, c.expected, found)
})
}
}
func TestNewDefaultRestartPolicyInSync(t *testing.T) {
cases := []struct {
typ string
expected structs.RestartPolicy
}{
{"service", structs.DefaultServiceJobRestartPolicy},
{"batch", structs.DefaultBatchJobRestartPolicy},
{"system", structs.DefaultServiceJobRestartPolicy},
}
for _, c := range cases {
t.Run(c.typ, func(t *testing.T) {
job := api.Job{Type: &c.typ}
var tg api.TaskGroup
tg.Canonicalize(&job)
apiP := tg.RestartPolicy
var found structs.RestartPolicy
toStructs(t, &found, apiP)
require.EqualValues(t, c.expected, found)
})
}
}
func toStructsResource(t *testing.T, in *api.Resources) structs.Resources {
var out structs.Resources
toStructs(t, &out, in)
return out
}
func toStructs(t *testing.T, out, in interface{}) {
bytes, err := json.Marshal(in)
require.NoError(t, err)
err = json.Unmarshal(bytes, &out)
require.NoError(t, err)
}