github.com/jiasir/deis@v1.12.2/builder/utils_test.go (about)

     1  package builder
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"strings"
     7  	"testing"
     8  	"time"
     9  
    10  	dtime "github.com/deis/deis/pkg/time"
    11  )
    12  
    13  type ClosingBuffer struct {
    14  	*bytes.Buffer
    15  }
    16  
    17  func (cb *ClosingBuffer) Close() error {
    18  	// we don't have to do anything here, since the buffer is just some data in memory
    19  	return nil
    20  }
    21  
    22  func stringInSlice(list []string, s string) bool {
    23  	for _, li := range list {
    24  		if li == s {
    25  			return true
    26  		}
    27  	}
    28  	return false
    29  }
    30  
    31  func TestYamlToJSONGood(t *testing.T) {
    32  	goodProcfiles := [][]byte{
    33  		[]byte(`web: while true; do echo hello; sleep 1; done`),
    34  
    35  		[]byte(`web: while true; do echo hello; sleep 1; done
    36  worker: while true; do echo hello; sleep 1; done`),
    37  		// test a procfile with quoted strings
    38  		[]byte(`web: /bin/bash -c "while true; do echo hello; sleep 1; done"`),
    39  	}
    40  
    41  	goodProcess := "while true; do echo hello; sleep 1; done"
    42  
    43  	for _, procfile := range goodProcfiles {
    44  		data, err := YamlToJSON(procfile)
    45  		if err != nil {
    46  			t.Errorf("expected procfile to be valid, got '%v'", err)
    47  		}
    48  		var p ProcessType
    49  		if err := json.Unmarshal([]byte(data), &p); err != nil {
    50  			t.Errorf("expected to be able to unmarshal object, got '%v'", err)
    51  		}
    52  		if !strings.Contains(p["web"], goodProcess) {
    53  			t.Errorf("expected web process == '%s', got '%s'", goodProcess, p["web"])
    54  		}
    55  	}
    56  }
    57  
    58  func TestParseConfigGood(t *testing.T) {
    59  	// mock the controller response
    60  	resp := bytes.NewBufferString(`{"owner": "test",
    61  			"app": "example-go",
    62  			"values": {"FOO": "bar", "CAR": 1234},
    63  			"memory": {},
    64  			"cpu": {},
    65  			"tags": {},
    66  			"created": "2014-01-01T00:00:00UTC",
    67  			"updated": "2014-01-01T00:00:00UTC",
    68  			"uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75"}`)
    69  
    70  	config, err := ParseConfig(resp.Bytes())
    71  
    72  	if err != nil {
    73  		t.Error(err)
    74  	}
    75  
    76  	if config.Values["FOO"] != "bar" {
    77  		t.Errorf("expected FOO='bar', got FOO='%v'", config.Values["FOO"])
    78  	}
    79  
    80  	if car, ok := config.Values["CAR"].(float64); ok {
    81  		if car != 1234 {
    82  			t.Errorf("expected CAR=1234, got CAR=%d", config.Values["CAR"])
    83  		}
    84  	} else {
    85  		t.Error("expected CAR to be of type float64")
    86  	}
    87  }
    88  
    89  func TestParseDomainGood(t *testing.T) {
    90  	// mock controller build-hook response
    91  	resp := []byte(`{"release": {"version": 1},
    92  "domains": ["test.example.com", "test2.example.com"]}`)
    93  
    94  	domain, err := ParseDomain(resp)
    95  	if err != nil {
    96  		t.Errorf("expected to parse domain, got '%v'", err)
    97  	}
    98  	if domain != "test.example.com" {
    99  		t.Errorf("expected 'test.example.com', got '%s'", domain)
   100  	}
   101  }
   102  
   103  func TestParseReleaseVersionGood(t *testing.T) {
   104  	// mock controller build-hook response
   105  	resp := []byte(`{"release": {"version": 1},
   106  "domains": ["test.example.com", "test2.example.com"]}`)
   107  
   108  	version, err := ParseReleaseVersion(resp)
   109  	if err != nil {
   110  		t.Errorf("expected to parse version, got '%v'", err)
   111  	}
   112  	if version != 1 {
   113  		t.Errorf("expected '1', got '%d'", version)
   114  	}
   115  }
   116  
   117  func TestGetDefaultTypeGood(t *testing.T) {
   118  	goodData := [][]byte{[]byte(`default_process_types:
   119    web: while true; do echo hello; sleep 1; done`),
   120  		[]byte(`foo: bar
   121  default_process_types:
   122    web: while true; do echo hello; sleep 1; done`),
   123  		[]byte(``)}
   124  
   125  	for _, data := range goodData {
   126  		defaultType, err := GetDefaultType(data)
   127  		if err != nil {
   128  			t.Error(err)
   129  		}
   130  		if defaultType != `{"web":"while true; do echo hello; sleep 1; done"}` && string(data) != "" {
   131  			t.Errorf("incorrect default type, got %s", defaultType)
   132  		}
   133  		if string(data) == "" && defaultType != "{}" {
   134  			t.Errorf("incorrect default type, got %s", defaultType)
   135  		}
   136  	}
   137  }
   138  
   139  func TestParseControllerConfigGood(t *testing.T) {
   140  	// mock controller config response
   141  	resp := []byte(`{"owner": "test",
   142  		"app": "example-go",
   143  		"values": {"FOO": "bar", "CAR": "star"},
   144  		"memory": {},
   145  		"cpu": {},
   146  		"tags": {},
   147  		"created": "2014-01-01T00:00:00UTC",
   148  		"updated": "2014-01-01T00:00:00UTC",
   149  		"uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75"
   150  	}`)
   151  
   152  	config, err := ParseControllerConfig(resp)
   153  
   154  	if err != nil {
   155  		t.Errorf("expected to pass, got '%v'", err)
   156  	}
   157  
   158  	if len(config) != 2 {
   159  		t.Errorf("expected 2, got %d", len(config))
   160  	}
   161  
   162  	if !stringInSlice(config, " -e CAR=\"star\"") {
   163  		t.Error("expected ' -e CAR=\"star\"' in slice")
   164  	}
   165  }
   166  
   167  func TestTimeSerialize(t *testing.T) {
   168  	time, err := json.Marshal(&dtime.Time{Time: time.Now().UTC()})
   169  
   170  	if err != nil {
   171  		t.Errorf("expected to be able to serialize time as json, got '%v'", err)
   172  	}
   173  
   174  	if !strings.Contains(string(time), "UTC") {
   175  		t.Errorf("could not find 'UTC' in datetime, got '%s'", string(time))
   176  	}
   177  }