forked from kedgeproject/kedge
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
populate name if one service specified, add tests
If only one root level service is defined, then the resulting Kubernetes service will have the name populated as the root level name field. The same is being done in case of root level persistent volume. Also, tests have been added for this behavior. Fixes kedgeproject#33
- Loading branch information
Showing
8 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package encoding | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/surajssd/kapp/pkg/encoding/fixtures" | ||
"github.com/surajssd/kapp/pkg/spec" | ||
|
||
"reflect" | ||
|
||
"github.com/davecgh/go-spew/spew" | ||
) | ||
|
||
func TestDecode(t *testing.T) { | ||
tests := []struct { | ||
Name string | ||
Data []byte | ||
App *spec.App | ||
}{ | ||
{ | ||
Name: "One container mentioned in the spec", | ||
Data: fixtures.SingleContainer, | ||
App: &fixtures.SingleContainerApp, | ||
}, | ||
{ | ||
Name: "One persistent volume mentioned in the spec", | ||
Data: fixtures.SinglePersistentVolume, | ||
App: &fixtures.SinglePersistentVolumeApp, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.Name, func(t *testing.T) { | ||
app, err := Decode(test.Data) | ||
if err != nil { | ||
t.Fatalf("Unable to run Decode(), and error occurred: %v", err) | ||
} | ||
|
||
if !reflect.DeepEqual(test.App, app) { | ||
t.Fatalf("Expected:\n%v\nGot:\n%v", spew.Sprint(test.App), spew.Sprint(app)) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package fixtures | ||
|
||
var SingleContainer []byte = []byte( | ||
`name: test | ||
containers: | ||
- image: nginx | ||
services: | ||
- ports: | ||
- port: 8080 | ||
`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package fixtures | ||
|
||
import ( | ||
"github.com/surajssd/kapp/pkg/spec" | ||
api_v1 "k8s.io/client-go/pkg/api/v1" | ||
) | ||
|
||
var SingleContainerApp spec.App = spec.App{ | ||
Name: "test", | ||
Containers: []spec.Container{ | ||
{ | ||
Container: api_v1.Container{ | ||
Image: "nginx", | ||
}, | ||
}, | ||
}, | ||
Services: []spec.ServiceSpecMod{ | ||
{ | ||
Ports: []spec.ServicePortMod{ | ||
{ | ||
ServicePort: api_v1.ServicePort{ | ||
Port: 8080, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package fixtures | ||
|
||
var SinglePersistentVolume []byte = []byte( | ||
`name: test | ||
containers: | ||
- image: nginx | ||
services: | ||
- ports: | ||
- port: 8080 | ||
persistentVolumes: | ||
- size: 500Mi | ||
`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package fixtures | ||
|
||
import ( | ||
"github.com/surajssd/kapp/pkg/spec" | ||
api_v1 "k8s.io/client-go/pkg/api/v1" | ||
) | ||
|
||
var SinglePersistentVolumeApp spec.App = spec.App{ | ||
Name: "test", | ||
Containers: []spec.Container{ | ||
{ | ||
Container: api_v1.Container{ | ||
Image: "nginx", | ||
}, | ||
}, | ||
}, | ||
Services: []spec.ServiceSpecMod{ | ||
{ | ||
Ports: []spec.ServicePortMod{ | ||
{ | ||
ServicePort: api_v1.ServicePort{ | ||
Port: 8080, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
PersistentVolumes: []spec.PersistentVolume{ | ||
{ | ||
Size: "500Mi", | ||
}, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package fixtures | ||
|
||
import api_v1 "k8s.io/client-go/pkg/api/v1" | ||
|
||
var SingleContainerService *api_v1.Service = &api_v1.Service{ | ||
ObjectMeta: api_v1.ObjectMeta{ | ||
Name: "test", | ||
}, | ||
Spec: api_v1.ServiceSpec{ | ||
Ports: []api_v1.ServicePort{ | ||
{ | ||
Port: 8080, | ||
}, | ||
}, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package kubernetes | ||
|
||
import ( | ||
"testing" | ||
|
||
"reflect" | ||
|
||
encodingFixtures "github.com/surajssd/kapp/pkg/encoding/fixtures" | ||
"github.com/surajssd/kapp/pkg/spec" | ||
transformFixtures "github.com/surajssd/kapp/pkg/transform/fixtures" | ||
"k8s.io/client-go/pkg/runtime" | ||
) | ||
|
||
func TestCreateServices(t *testing.T) { | ||
tests := []struct { | ||
Name string | ||
App *spec.App | ||
Objects []runtime.Object | ||
}{ | ||
{ | ||
"Single container specified", | ||
&encodingFixtures.SingleContainerApp, | ||
append(make([]runtime.Object, 0), transformFixtures.SingleContainerService), | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.Name, func(t *testing.T) { | ||
object, err := createServices(test.App) | ||
if err != nil { | ||
t.Fatalf("Creating services failed: %v", err) | ||
} | ||
if !reflect.DeepEqual(test.Objects, object) { | ||
t.Fatalf("Expected:\n%v\nGot:\n%v", test.Objects, object) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
// TODO: add test for auto naming of single persistent volume |