...

Source file src/edge-infra.dev/pkg/edge/api/services/channels/parsing_test.go

Documentation: edge-infra.dev/pkg/edge/api/services/channels

     1  package channels
     2  
     3  import (
     4  	_ "embed"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  var (
    11  	//go:embed testdata/config-without-channel.yaml
    12  	configWithoutChannel []byte
    13  	//go:embed testdata/config-with-valid-channel.yaml
    14  	configWithValidChannel []byte
    15  	//go:embed testdata/config-with-invalid-channel.yaml
    16  	configWithInvalidChannel []byte
    17  )
    18  
    19  func TestParseChannelFromHelmConfig(t *testing.T) {
    20  	var invalidYaml = append(configWithoutChannel, '\n', '{')
    21  
    22  	testCases := []struct {
    23  		title      string
    24  		helmConfig string
    25  		expected   []string // names expected
    26  		expectErr  bool
    27  	}{
    28  		{
    29  			title:      "Helm config is an empty string",
    30  			helmConfig: "",
    31  			expected:   []string{},
    32  			expectErr:  false,
    33  		},
    34  		{
    35  			title:      "Helm config does not contain channel info",
    36  			helmConfig: string(configWithoutChannel),
    37  			expected:   []string{},
    38  			expectErr:  false,
    39  		},
    40  		{
    41  			title:      "Helm config contains a valid channel",
    42  			helmConfig: string(configWithValidChannel),
    43  			expected:   []string{"test-name", "test-name-2"},
    44  			expectErr:  false,
    45  		},
    46  		{
    47  			title:      "Helm config is not valid yaml",
    48  			helmConfig: string(invalidYaml),
    49  			expectErr:  true,
    50  		},
    51  		{
    52  			title:      "Helm config does not contain a valid channel (missing bannerEdgeID)",
    53  			helmConfig: string(configWithInvalidChannel),
    54  			expectErr:  true,
    55  		},
    56  	}
    57  
    58  	for _, tc := range testCases {
    59  		t.Run(tc.title, func(t *testing.T) {
    60  			got, err := ParseHelmConfigChannels(tc.helmConfig)
    61  			if err != nil {
    62  				assert.True(t, tc.expectErr)
    63  				return
    64  			}
    65  
    66  			assert.False(t, tc.expectErr)
    67  			assert.Len(t, got.Channels, len(tc.expected))
    68  			for _, channel := range got.Channels {
    69  				assert.Contains(t, tc.expected, channel.Name)
    70  			}
    71  		})
    72  	}
    73  }
    74  

View as plain text