github.com/pwn-term/docker@v0.0.0-20210616085119-6e977cce2565/cli/kubernetes/check_test.go (about)

     1  package kubernetes
     2  
     3  import (
     4  	"testing"
     5  
     6  	"gotest.tools/v3/assert"
     7  	is "gotest.tools/v3/assert/cmp"
     8  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
     9  )
    10  
    11  func TestGetStackAPIVersion(t *testing.T) {
    12  	var tests = []struct {
    13  		description   string
    14  		groups        *metav1.APIGroupList
    15  		err           bool
    16  		expectedStack StackVersion
    17  	}{
    18  		{"no stack api", makeGroups(), true, ""},
    19  		{"v1beta1", makeGroups(groupVersion{"compose.docker.com", []string{"v1beta1"}}), false, StackAPIV1Beta1},
    20  		{"v1beta2", makeGroups(groupVersion{"compose.docker.com", []string{"v1beta2"}}), false, StackAPIV1Beta2},
    21  		{"most recent has precedence", makeGroups(groupVersion{"compose.docker.com", []string{"v1beta1", "v1beta2"}}), false, StackAPIV1Beta2},
    22  		{"most recent has precedence", makeGroups(groupVersion{"compose.docker.com", []string{"v1beta1", "v1beta2", "v1alpha3"}}), false, StackAPIV1Alpha3},
    23  	}
    24  
    25  	for _, test := range tests {
    26  		version, err := getAPIVersion(test.groups)
    27  		if test.err {
    28  			assert.ErrorContains(t, err, "")
    29  		} else {
    30  			assert.NilError(t, err)
    31  		}
    32  		assert.Check(t, is.Equal(test.expectedStack, version))
    33  	}
    34  }
    35  
    36  type groupVersion struct {
    37  	name     string
    38  	versions []string
    39  }
    40  
    41  func makeGroups(versions ...groupVersion) *metav1.APIGroupList {
    42  	groups := make([]metav1.APIGroup, len(versions))
    43  	for i := range versions {
    44  		groups[i].Name = versions[i].name
    45  		for _, v := range versions[i].versions {
    46  			groups[i].Versions = append(groups[i].Versions, metav1.GroupVersionForDiscovery{Version: v})
    47  		}
    48  	}
    49  	return &metav1.APIGroupList{
    50  		Groups: groups,
    51  	}
    52  }