github.com/jenkins-x/draft-repo@v0.9.0/pkg/draft/local/local_test.go (about)

     1  package local
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"k8s.io/client-go/pkg/api/v1"
     8  )
     9  
    10  func TestDeployedApplication(t *testing.T) {
    11  	expectedApp := &App{
    12  		Name:      "example-app",
    13  		Namespace: "example-namespace",
    14  	}
    15  
    16  	app, err := DeployedApplication("../testdata/app/draft.toml", "development")
    17  	if err != nil {
    18  		t.Fatal(err)
    19  	}
    20  
    21  	if !reflect.DeepEqual(expectedApp, app) {
    22  		t.Errorf("Expected %#v, got %#v", expectedApp, app)
    23  	}
    24  }
    25  
    26  func TestGetContainerPort(t *testing.T) {
    27  	containersTest1 := []v1.Container{
    28  		v1.Container{Name: "anothercontainer", Ports: []v1.ContainerPort{v1.ContainerPort{ContainerPort: 3000}}},
    29  		v1.Container{Name: "mycontainer", Ports: []v1.ContainerPort{v1.ContainerPort{ContainerPort: 4000}}},
    30  	}
    31  
    32  	testCases := []struct {
    33  		description     string
    34  		containers      []v1.Container
    35  		targetContainer string
    36  		expectedPort    int
    37  		expectErr       bool
    38  	}{
    39  		{"test correct container and port found", containersTest1, "mycontainer", 4000, false},
    40  		{"test first container and port found", containersTest1, "", 3000, false},
    41  		{"test container not found error", containersTest1, "randomcontainer", 0, true},
    42  	}
    43  
    44  	for _, tc := range testCases {
    45  		port, err := getContainerPort(tc.containers, tc.targetContainer)
    46  		if tc.expectErr && err == nil {
    47  			t.Errorf("Expected err but did not get one for case: %s", tc.description)
    48  		}
    49  
    50  		if tc.expectedPort != 0 && (tc.expectedPort != port) {
    51  			t.Errorf("Expected port %v, got %v for scenario: %s", tc.expectedPort, port, tc.description)
    52  		}
    53  	}
    54  }