github.com/technosophos/deis@v1.7.1-0.20150915173815-f9005256004b/deisctl/config/config_test.go (about)

     1  package config
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/base64"
     6  	"fmt"
     7  	"io/ioutil"
     8  	"os"
     9  	"testing"
    10  
    11  	"github.com/deis/deis/deisctl/config/model"
    12  	"github.com/deis/deis/deisctl/test/mock"
    13  )
    14  
    15  func TestGetConfig(t *testing.T) {
    16  	t.Parallel()
    17  
    18  	testMock := mock.ConfigBackend{Expected: []*model.ConfigNode{{Key: "/deis/controller/testing", Value: "foo"}, {Key: "/deis/controller/port", Value: "8000"}}}
    19  	testWriter := bytes.Buffer{}
    20  
    21  	err := doConfig("controller", "get", []string{"testing", "port"}, testMock, &testWriter)
    22  
    23  	if err != nil {
    24  		t.Fatal(err)
    25  	}
    26  
    27  	expected := "foo\n8000\n"
    28  	output := testWriter.String()
    29  	if output != expected {
    30  		t.Error(fmt.Errorf("Expected: '%s', Got:'%s'", expected, output))
    31  	}
    32  }
    33  
    34  func TestGetConfigError(t *testing.T) {
    35  	t.Parallel()
    36  
    37  	testMock := mock.ConfigBackend{Expected: []*model.ConfigNode{{Key: "/deis/controller/testing", Value: "foo"}}}
    38  	testWriter := bytes.Buffer{}
    39  
    40  	err := doConfig("controller", "get", []string{"port"}, testMock, &testWriter)
    41  
    42  	if err == nil {
    43  		t.Fatal("Error Expected")
    44  	}
    45  }
    46  
    47  func TestSetConfig(t *testing.T) {
    48  	t.Parallel()
    49  
    50  	testMock := mock.ConfigBackend{Expected: []*model.ConfigNode{{Key: "/deis/controller/testing", Value: "foo"}, {Key: "/deis/controller/port", Value: "8000"}}}
    51  	testWriter := bytes.Buffer{}
    52  
    53  	err := doConfig("controller", "set", []string{"testing=bar", "port=1000"}, testMock, &testWriter)
    54  
    55  	if err != nil {
    56  		t.Fatal(err)
    57  	}
    58  
    59  	expected := "bar\n1000\n"
    60  	output := testWriter.String()
    61  	if output != expected {
    62  		t.Error(fmt.Errorf("Expected: '%s', Got:'%s'", expected, output))
    63  	}
    64  }
    65  
    66  func TestDeleteConfig(t *testing.T) {
    67  	t.Parallel()
    68  
    69  	testMock := mock.ConfigBackend{Expected: []*model.ConfigNode{{Key: "/deis/controller/testing", Value: "foo"}, {Key: "/deis/controller/port", Value: "8000"}}}
    70  	testWriter := bytes.Buffer{}
    71  
    72  	err := doConfig("controller", "rm", []string{"testing", "port"}, testMock, &testWriter)
    73  
    74  	if err != nil {
    75  		t.Fatal(err)
    76  	}
    77  
    78  	expected := "testing\nport\n"
    79  	output := testWriter.String()
    80  	if output != expected {
    81  		t.Error(fmt.Errorf("Expected: '%s', Got:'%s'", expected, output))
    82  	}
    83  }
    84  
    85  // TestConfigSSHPrivateKey ensures private keys are base64 encoded from file path
    86  func TestConfigSSHPrivateKey(t *testing.T) {
    87  	t.Parallel()
    88  
    89  	f, err := writeTempFile("private-key")
    90  	if err != nil {
    91  		t.Fatal(err)
    92  	}
    93  
    94  	val, err := valueForPath("/deis/platform/sshPrivateKey", f.Name())
    95  	if err != nil {
    96  		t.Fatal(err)
    97  	}
    98  
    99  	encoded := base64.StdEncoding.EncodeToString([]byte("private-key"))
   100  
   101  	if val != encoded {
   102  		t.Fatalf("expected: %v, got: %v", encoded, val)
   103  	}
   104  }
   105  
   106  func TestConfigRouterKey(t *testing.T) {
   107  	t.Parallel()
   108  
   109  	f, err := writeTempFile("router-key")
   110  	if err != nil {
   111  		t.Fatal(err)
   112  	}
   113  
   114  	val, err := valueForPath("/deis/router/sslKey", f.Name())
   115  	if err != nil {
   116  		t.Fatal(err)
   117  	}
   118  
   119  	if val != "router-key" {
   120  		t.Fatalf("expected: router-key, got: %v", val)
   121  	}
   122  
   123  }
   124  
   125  func TestConfigRouterCert(t *testing.T) {
   126  	t.Parallel()
   127  
   128  	f, err := writeTempFile("router-cert")
   129  	if err != nil {
   130  		t.Fatal(err)
   131  	}
   132  
   133  	val, err := valueForPath("/deis/router/sslCert", f.Name())
   134  	if err != nil {
   135  		t.Fatal(err)
   136  	}
   137  
   138  	if val != "router-cert" {
   139  		t.Fatalf("expected: router-cert, got: %v", val)
   140  	}
   141  
   142  }
   143  
   144  func writeTempFile(data string) (*os.File, error) {
   145  	f, err := ioutil.TempFile("", "deisctl")
   146  	if err != nil {
   147  		return nil, err
   148  	}
   149  
   150  	f.Write([]byte(data))
   151  	defer f.Close()
   152  
   153  	return f, nil
   154  }