This repository has been archived by the owner on Jan 29, 2019. It is now read-only.
/
api_test.go
78 lines (63 loc) · 1.73 KB
/
api_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"crypto/sha1"
"encoding/hex"
"encoding/json"
"github.com/apex/log"
"github.com/apex/log/handlers/cli"
"github.com/go-martini/martini"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"reflect"
"testing"
)
func TestHttpWorking(t *testing.T) {
// This is just an example
m := martini.Classic()
m.Get("/foo", func() string {
return "bar"
})
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/foo", nil)
m.ServeHTTP(res, req)
expect(t, res.Code, http.StatusOK)
expect(t, res.Body.String(), `bar`)
}
/* Test Helpers */
func expect(t *testing.T, a interface{}, b interface{}) {
if a != b {
t.Errorf("Expected %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a))
}
}
func TestDockerfileCreation(t *testing.T) {
log.SetHandler(cli.New(os.Stdout))
// The 'expected' hash
expected_hash := "033429aed5b9104f5c8d0a15ed2b9a043ce93a70"
// First we will read the sample json file
dat, err := ioutil.ReadFile("test/sample_data.json")
if err != nil {
log.WithError(err)
}
// We read the sample json data and create a new DockerInfo struct
var parsed_data DockerInfo
err = json.Unmarshal(dat, &parsed_data)
if err != nil {
log.WithError(err)
}
// Pass the DockerInfo struct into the GenerateDockerFile function
GenerateDockerFile(parsed_data, "test")
defer os.Remove("test/Dockerfile")
// Generate a sha1 hash of the generated Dockerfile and compare
f, err := ioutil.ReadFile("test/Dockerfile")
if err != nil {
log.WithError(err)
}
generated_hash := sha1.New()
generated_hash.Write([]byte(f))
bs := generated_hash.Sum(nil)
// We would like a hex-encoding string to compare with
hash_string := hex.EncodeToString(bs[:])
expect(t, hash_string, expected_hash)
}