github.com/skyscape-cloud-services/terraform@v0.9.2-0.20170609144644-7ece028a1747/builtin/providers/rancher/util_test.go (about)

     1  package rancher
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/rancher/go-rancher/v2"
     7  )
     8  
     9  var idTests = []struct {
    10  	id         string
    11  	envID      string
    12  	resourceID string
    13  }{
    14  	{"1a05", "", "1a05"},
    15  	{"1a05/1s234", "1a05", "1s234"},
    16  }
    17  
    18  func TestSplitId(t *testing.T) {
    19  	for _, tt := range idTests {
    20  		envID, resourceID := splitID(tt.id)
    21  		if envID != tt.envID || resourceID != tt.resourceID {
    22  			t.Errorf("splitId(%s) => [%s, %s]) want [%s, %s]", tt.id, envID, resourceID, tt.envID, tt.resourceID)
    23  		}
    24  	}
    25  }
    26  
    27  var stateTests = []struct {
    28  	state   string
    29  	removed bool
    30  }{
    31  	{"removed", true},
    32  	{"purged", true},
    33  	{"active", false},
    34  }
    35  
    36  func TestRemovedState(t *testing.T) {
    37  	for _, tt := range stateTests {
    38  		removed := removed(tt.state)
    39  		if removed != tt.removed {
    40  			t.Errorf("removed(%s) => %t, wants %t", tt.state, removed, tt.removed)
    41  		}
    42  	}
    43  }
    44  
    45  var orchestrationTests = []struct {
    46  	project       *client.Project
    47  	orchestration string
    48  }{
    49  	{&client.Project{Orchestration: "cattle"}, "cattle"},
    50  	{&client.Project{Orchestration: "swarm"}, "swarm"},
    51  	{&client.Project{Orchestration: "mesos"}, "mesos"},
    52  	{&client.Project{Orchestration: "kubernetes"}, "kubernetes"},
    53  }
    54  
    55  func TestActiveOrchestration(t *testing.T) {
    56  	for _, tt := range orchestrationTests {
    57  		orchestration := getActiveOrchestration(tt.project)
    58  		if orchestration != tt.orchestration {
    59  			t.Errorf("getActiveOrchestration(%+v) => %s, wants %s", tt.project, orchestration, tt.orchestration)
    60  		}
    61  	}
    62  }
    63  
    64  type LabelTestCase struct {
    65  	Labels          map[string]interface{}
    66  	Command         string
    67  	ExpectedCommand string
    68  }
    69  
    70  var (
    71  	HostLabelTestCases = []LabelTestCase{
    72  		LabelTestCase{
    73  			Labels: map[string]interface{}{
    74  				"orch": "true",
    75  				"etcd": "true",
    76  			},
    77  			Command:         "sudo docker run --rm --privileged -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/rancher:/var/lib/rancher rancher/agent:v1.2.2 http://192.168.122.158:8080/v1/scripts/71FF294EA7A2B6865708:1483142400000:8OVFmSEUlS2VXvVGbYCXTFaMC8w",
    78  			ExpectedCommand: "sudo docker run -e CATTLE_HOST_LABELS='etcd=true&orch=true' --rm --privileged -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/rancher:/var/lib/rancher rancher/agent:v1.2.2 http://192.168.122.158:8080/v1/scripts/71FF294EA7A2B6865708:1483142400000:8OVFmSEUlS2VXvVGbYCXTFaMC8w",
    79  		},
    80  		LabelTestCase{
    81  			Labels:          map[string]interface{}{},
    82  			Command:         "sudo docker run --rm --privileged -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/rancher:/var/lib/rancher rancher/agent:v1.2.2 http://192.168.122.158:8080/v1/scripts/71FF294EA7A2B6865708:1483142400000:8OVFmSEUlS2VXvVGbYCXTFaMC8w",
    83  			ExpectedCommand: "sudo docker run --rm --privileged -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/rancher:/var/lib/rancher rancher/agent:v1.2.2 http://192.168.122.158:8080/v1/scripts/71FF294EA7A2B6865708:1483142400000:8OVFmSEUlS2VXvVGbYCXTFaMC8w",
    84  		},
    85  	}
    86  )
    87  
    88  func TestAddHostLabels(t *testing.T) {
    89  	for _, tCase := range HostLabelTestCases {
    90  		cmd := addHostLabels(tCase.Command, tCase.Labels)
    91  		if cmd != tCase.ExpectedCommand {
    92  			t.Errorf("Command:\n%s\nDoes not match\n%s", cmd, tCase.ExpectedCommand)
    93  		}
    94  	}
    95  }