github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/internal/testing/apitests/structsync_test.go (about)

     1  package apitests
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/nomad/api"
     8  	"github.com/hashicorp/nomad/ci"
     9  	"github.com/hashicorp/nomad/nomad/structs"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  // Tests that api and struct values are equivalent
    14  //
    15  // Given that vendoring libraries prune tests by default, test dependencies
    16  // aren't leaked to clients of the package - so it should be safe to add
    17  // such dependency without affecting api clients.
    18  
    19  func TestDefaultResourcesAreInSync(t *testing.T) {
    20  	ci.Parallel(t)
    21  
    22  	apiR := api.DefaultResources()
    23  	structsR := structs.DefaultResources()
    24  
    25  	require.EqualValues(t, *structsR, toStructsResource(t, apiR))
    26  
    27  	// match after canonicalization
    28  	apiR.Canonicalize()
    29  	structsR.Canonicalize()
    30  	require.EqualValues(t, *structsR, toStructsResource(t, apiR))
    31  }
    32  
    33  func TestMinResourcesAreInSync(t *testing.T) {
    34  	ci.Parallel(t)
    35  
    36  	apiR := api.MinResources()
    37  	structsR := structs.MinResources()
    38  
    39  	require.EqualValues(t, *structsR, toStructsResource(t, apiR))
    40  
    41  	// match after canonicalization
    42  	apiR.Canonicalize()
    43  	structsR.Canonicalize()
    44  	require.EqualValues(t, *structsR, toStructsResource(t, apiR))
    45  }
    46  
    47  func TestNewDefaultRescheulePolicyInSync(t *testing.T) {
    48  	ci.Parallel(t)
    49  
    50  	cases := []struct {
    51  		typ      string
    52  		expected structs.ReschedulePolicy
    53  	}{
    54  		{"service", structs.DefaultServiceJobReschedulePolicy},
    55  		{"batch", structs.DefaultBatchJobReschedulePolicy},
    56  		{"system", structs.ReschedulePolicy{}},
    57  	}
    58  
    59  	for _, c := range cases {
    60  		t.Run(c.typ, func(t *testing.T) {
    61  			apiP := api.NewDefaultReschedulePolicy(c.typ)
    62  
    63  			var found structs.ReschedulePolicy
    64  			toStructs(t, &found, apiP)
    65  
    66  			require.EqualValues(t, c.expected, found)
    67  		})
    68  	}
    69  }
    70  
    71  func TestNewDefaultRestartPolicyInSync(t *testing.T) {
    72  	ci.Parallel(t)
    73  
    74  	cases := []struct {
    75  		typ      string
    76  		expected structs.RestartPolicy
    77  	}{
    78  		{"service", structs.DefaultServiceJobRestartPolicy},
    79  		{"batch", structs.DefaultBatchJobRestartPolicy},
    80  		{"system", structs.DefaultServiceJobRestartPolicy},
    81  	}
    82  
    83  	for _, c := range cases {
    84  		t.Run(c.typ, func(t *testing.T) {
    85  			job := api.Job{Type: &c.typ}
    86  			var tg api.TaskGroup
    87  			tg.Canonicalize(&job)
    88  
    89  			apiP := tg.RestartPolicy
    90  
    91  			var found structs.RestartPolicy
    92  			toStructs(t, &found, apiP)
    93  
    94  			require.EqualValues(t, c.expected, found)
    95  		})
    96  	}
    97  }
    98  
    99  func toStructsResource(t *testing.T, in *api.Resources) structs.Resources {
   100  	var out structs.Resources
   101  	toStructs(t, &out, in)
   102  	return out
   103  }
   104  
   105  func toStructs(t *testing.T, out, in interface{}) {
   106  	bytes, err := json.Marshal(in)
   107  	require.NoError(t, err)
   108  
   109  	err = json.Unmarshal(bytes, &out)
   110  	require.NoError(t, err)
   111  }