2019-03-29 18:47:40 +00:00
|
|
|
package apitests
|
2019-01-19 16:19:58 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
|
2019-03-29 18:47:40 +00:00
|
|
|
"github.com/hashicorp/nomad/api"
|
2019-01-19 16:19:58 +00:00
|
|
|
"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) {
|
2019-03-29 18:47:40 +00:00
|
|
|
apiR := api.DefaultResources()
|
2019-01-19 16:19:58 +00:00
|
|
|
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) {
|
2019-03-29 18:47:40 +00:00
|
|
|
apiR := api.MinResources()
|
2019-01-19 16:19:58 +00:00
|
|
|
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) {
|
2019-03-29 18:47:40 +00:00
|
|
|
apiP := api.NewDefaultReschedulePolicy(c.typ)
|
2019-01-19 16:19:58 +00:00
|
|
|
|
|
|
|
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) {
|
2019-03-29 18:47:40 +00:00
|
|
|
job := api.Job{Type: &c.typ}
|
|
|
|
var tg api.TaskGroup
|
2019-01-19 16:19:58 +00:00
|
|
|
tg.Canonicalize(&job)
|
|
|
|
|
|
|
|
apiP := tg.RestartPolicy
|
|
|
|
|
|
|
|
var found structs.RestartPolicy
|
|
|
|
toStructs(t, &found, apiP)
|
|
|
|
|
|
|
|
require.EqualValues(t, c.expected, found)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-29 18:47:40 +00:00
|
|
|
func toStructsResource(t *testing.T, in *api.Resources) structs.Resources {
|
2019-01-19 16:19:58 +00:00
|
|
|
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)
|
|
|
|
}
|