11package drycc
22
3- import "testing"
3+ import (
4+ "fmt"
5+ "os"
6+ "path/filepath"
7+ "testing"
8+ )
49
510type versionComparison struct {
611 Client string
@@ -17,10 +22,135 @@ func TestCheckAPIVersions(t *testing.T) {
1722 }
1823
1924 for _ , check := range comparisons {
20- err := checkAPICompatibility (check .Client , check .Server )
25+ err := CheckAPICompatibility (check .Client , check .Server )
2126
2227 if err != check .Error {
2328 t .Errorf ("%v: Expected %v, Got %v" , check , check .Error , err )
2429 }
2530 }
2631}
32+
33+ func TestParseEnv (t * testing.T ) {
34+ expects := map [string ]string {
35+ "F1" : "111" ,
36+ "F2" : "222" ,
37+ "F3" : "333" ,
38+ }
39+
40+ f , err := os .CreateTemp ("" , "env" )
41+ if err != nil {
42+ t .Fatal (err )
43+ }
44+ defer os .RemoveAll (f .Name ())
45+ for key , value := range expects {
46+ f .WriteString (fmt .Sprintf ("%s=%s\n " , key , value ))
47+ }
48+ f .Seek (0 , 0 )
49+
50+ config , err := ParseEnv (f .Name ())
51+ if err != nil {
52+ t .Fatal (err )
53+ }
54+ if len (config ) != 3 {
55+ t .Errorf ("Expected %d, Got %d" , 3 , len (config ))
56+ }
57+ for key , value := range expects {
58+ if v , ok := config [key ]; ! ok || config [key ] != value {
59+ t .Errorf ("Expected %s, Got %s" , v , value )
60+ }
61+ }
62+ }
63+
64+ func TestParseDryccfile (t * testing.T ) {
65+ webPipeline := `
66+ kind: pipeline
67+ ptype: web
68+ build:
69+ docker: Dockerfile
70+ arg:
71+ CODENAME: bookworm
72+ env:
73+ VERSION: 1.2.1
74+ run:
75+ command:
76+ - ./deployment-tasks.sh
77+ image: worker
78+ timeout: 100
79+ config:
80+ - jvmconfig
81+ deploy:
82+ command:
83+ - bash
84+ - -ec
85+ args:
86+ - bundle exec puma -C config/puma.rb
87+ `
88+ taskPipeline := `
89+ kind: pipeline
90+ ptype: task
91+ build:
92+ docker: Dockerfile
93+ arg:
94+ CODENAME: bookworm
95+ env:
96+ VERSION: 1.2.1
97+ run:
98+ command:
99+ - ./deployment-tasks.sh
100+ image: worker
101+ timeout: 100
102+ config:
103+ - jvmconfig
104+ deploy:
105+ command:
106+ - bash
107+ - -ec
108+ args:
109+ - bundle exec puma -C config/puma.rb
110+ `
111+
112+ tmp , err := os .MkdirTemp ("" , "drycc" )
113+ if err != nil {
114+ t .Fatal (err )
115+ }
116+ defer os .RemoveAll (tmp )
117+
118+ configDir := filepath .Join (tmp , "config" )
119+ os .MkdirAll (configDir , 0777 )
120+ os .WriteFile (filepath .Join (tmp , "web.yml" ), []byte (webPipeline ), 0777 )
121+ os .WriteFile (filepath .Join (tmp , "task.yaml" ), []byte (taskPipeline ), 0777 )
122+ os .WriteFile (filepath .Join (configDir , "global" ), []byte ("DEBUG=true\n " ), 0777 )
123+ os .WriteFile (filepath .Join (configDir , "web" ), []byte ("PORT=8000\n JVM_OPTIONS=-Xms16G\n " ), 0777 )
124+
125+ dryccfile , err := ParseDryccfile (tmp )
126+ if err != nil {
127+ t .Fatal (err )
128+ }
129+ if config , ok := dryccfile ["config" ]; ok {
130+ webConfig := config .(map [string ]interface {})["web" ].(map [string ]interface {})
131+ globalConfig := config .(map [string ]interface {})["global" ].(map [string ]interface {})
132+
133+ if globalConfig ["DEBUG" ] != "true" {
134+ t .Errorf ("Expected %s, Got %s" , "true" , globalConfig ["DEBUG" ])
135+ }
136+ if webConfig ["PORT" ].(string ) != "8000" {
137+ t .Errorf ("Expected %s, Got %s" , "8000" , webConfig ["PORT" ])
138+ }
139+ if webConfig ["JVM_OPTIONS" ].(string ) != "-Xms16G" {
140+ t .Errorf ("Expected %s, Got %s" , "-Xms16G" , webConfig ["JVM_OPTIONS" ])
141+ }
142+ } else {
143+ t .Error ("Config not found" )
144+ }
145+
146+ if pipeline , ok := dryccfile ["pipeline" ]; ok {
147+ if _ , ok := pipeline .(map [string ]interface {})["web.yml" ]; ! ok {
148+ t .Error ("web.yml not found" )
149+ }
150+ if _ , ok := pipeline .(map [string ]interface {})["task.yaml" ]; ! ok {
151+ t .Error ("task.yaml not found" )
152+ }
153+ } else {
154+ t .Error ("Pipeline not found" )
155+ }
156+ }
0 commit comments