github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/nomad/structs/alloc_test.go (about)

     1  package structs
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  )
     8  
     9  func TestAllocServiceRegistrationsRequest_StaleReadSupport(t *testing.T) {
    10  	req := &AllocServiceRegistrationsRequest{}
    11  	require.True(t, req.IsRead())
    12  }
    13  
    14  func Test_Allocation_ServiceProviderNamespace(t *testing.T) {
    15  	testCases := []struct {
    16  		inputAllocation *Allocation
    17  		expectedOutput  string
    18  		name            string
    19  	}{
    20  		{
    21  			inputAllocation: &Allocation{
    22  				Job: &Job{
    23  					TaskGroups: []*TaskGroup{
    24  						{
    25  							Name: "test-group",
    26  							Services: []*Service{
    27  								{
    28  									Provider: ServiceProviderConsul,
    29  								},
    30  							},
    31  						},
    32  					},
    33  				},
    34  				TaskGroup: "test-group",
    35  			},
    36  			expectedOutput: "",
    37  			name:           "consul task group service",
    38  		},
    39  		{
    40  			inputAllocation: &Allocation{
    41  				Job: &Job{
    42  					TaskGroups: []*TaskGroup{
    43  						{
    44  							Name: "test-group",
    45  							Tasks: []*Task{
    46  								{
    47  									Services: []*Service{
    48  										{
    49  											Provider: ServiceProviderConsul,
    50  										},
    51  									},
    52  								},
    53  							},
    54  						},
    55  					},
    56  				},
    57  				TaskGroup: "test-group",
    58  			},
    59  			expectedOutput: "",
    60  			name:           "consul task service",
    61  		},
    62  		{
    63  			inputAllocation: &Allocation{
    64  				Job: &Job{
    65  					Namespace: "platform",
    66  					TaskGroups: []*TaskGroup{
    67  						{
    68  							Name: "test-group",
    69  							Services: []*Service{
    70  								{
    71  									Provider: ServiceProviderNomad,
    72  								},
    73  							},
    74  						},
    75  					},
    76  				},
    77  				TaskGroup: "test-group",
    78  			},
    79  			expectedOutput: "platform",
    80  			name:           "nomad task group service",
    81  		},
    82  		{
    83  			inputAllocation: &Allocation{
    84  				Job: &Job{
    85  					Namespace: "platform",
    86  					TaskGroups: []*TaskGroup{
    87  						{
    88  							Name: "test-group",
    89  							Tasks: []*Task{
    90  								{
    91  									Services: []*Service{
    92  										{
    93  											Provider: ServiceProviderNomad,
    94  										},
    95  									},
    96  								},
    97  							},
    98  						},
    99  					},
   100  				},
   101  				TaskGroup: "test-group",
   102  			},
   103  			expectedOutput: "platform",
   104  			name:           "nomad task service",
   105  		},
   106  		{
   107  			inputAllocation: &Allocation{
   108  				Job: &Job{
   109  					Namespace: "platform",
   110  					TaskGroups: []*TaskGroup{
   111  						{
   112  							Name: "test-group",
   113  							Tasks: []*Task{
   114  								{
   115  									Name: "task1",
   116  								},
   117  								{
   118  									Name: "task2",
   119  									Services: []*Service{
   120  										{
   121  											Provider: ServiceProviderNomad,
   122  										},
   123  									},
   124  								},
   125  							},
   126  						},
   127  					},
   128  				},
   129  				TaskGroup: "test-group",
   130  			},
   131  			expectedOutput: "platform",
   132  			name:           "multiple tasks with service not in first",
   133  		},
   134  	}
   135  
   136  	for _, tc := range testCases {
   137  		t.Run(tc.name, func(t *testing.T) {
   138  			actualOutput := tc.inputAllocation.ServiceProviderNamespace()
   139  			require.Equal(t, tc.expectedOutput, actualOutput)
   140  		})
   141  	}
   142  }