github.com/hernad/nomad@v1.6.112/nomad/structs/alloc_test.go (about)

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