Merge pull request #317 from ngtuna/env-key-only

support parse key-only environment variable
This commit is contained in:
Suraj Deshmukh 2016-12-19 22:51:06 +05:30 committed by GitHub
commit c485a870d8
6 changed files with 345 additions and 3 deletions

View File

@ -60,9 +60,14 @@ func loadEnvVars(envars []string) []kobject.EnvVar {
if character == "" { if character == "" {
envs = append(envs, kobject.EnvVar{ envs = append(envs, kobject.EnvVar{
Name: e, Name: e,
Value: os.Getenv(e),
}) })
} else { } else {
values := strings.SplitN(e, character, 2) values := strings.SplitN(e, character, 2)
// try to get value from os env
if values[1] == "" {
values[1] = os.Getenv(values[0])
}
envs = append(envs, kobject.EnvVar{ envs = append(envs, kobject.EnvVar{
Name: values[0], Name: values[0],
Value: values[1], Value: values[1],

View File

@ -16,7 +16,12 @@ limitations under the License.
package compose package compose
import "testing" import (
"os"
"testing"
"github.com/kubernetes-incubator/kompose/pkg/kobject"
)
// Test if service types are parsed properly on user input // Test if service types are parsed properly on user input
// give a service type and expect correct input // give a service type and expect correct input
@ -41,3 +46,69 @@ func TestHandleServiceType(t *testing.T) {
} }
} }
} }
func TestLoadEnvVar(t *testing.T) {
ev1 := []string{"foo=bar"}
rs1 := kobject.EnvVar{
Name: "foo",
Value: "bar",
}
ev2 := []string{"foo:bar"}
rs2 := kobject.EnvVar{
Name: "foo",
Value: "bar",
}
ev3 := []string{"foo"}
rs3 := kobject.EnvVar{
Name: "foo",
Value: "",
}
ev4 := []string{"osfoo"}
rs4 := kobject.EnvVar{
Name: "osfoo",
Value: "osbar",
}
ev5 := []string{"foo:bar=foobar"}
rs5 := kobject.EnvVar{
Name: "foo",
Value: "bar=foobar",
}
ev6 := []string{"foo=foo:bar"}
rs6 := kobject.EnvVar{
Name: "foo",
Value: "foo:bar",
}
ev7 := []string{"foo:"}
rs7 := kobject.EnvVar{
Name: "foo",
Value: "",
}
ev8 := []string{"foo="}
rs8 := kobject.EnvVar{
Name: "foo",
Value: "",
}
tests := []struct {
envvars []string
results kobject.EnvVar
}{
{ev1, rs1},
{ev2, rs2},
{ev3, rs3},
{ev4, rs4},
{ev5, rs5},
{ev6, rs6},
{ev7, rs7},
{ev8, rs8},
}
os.Setenv("osfoo", "osbar")
for _, tt := range tests {
result := loadEnvVars(tt.envvars)
if result[0] != tt.results {
t.Errorf("Expected %q, got %q", tt.results, result[0])
}
}
}

View File

@ -103,7 +103,6 @@ convert::expect_success "kompose --bundle $KOMPOSE_ROOT/script/test/fixtures/bun
# Test related to kompose --bundle convert to ensure that DSB bundles are converted properly # Test related to kompose --bundle convert to ensure that DSB bundles are converted properly
convert::expect_success_and_warning "kompose --bundle $KOMPOSE_ROOT/script/test/fixtures/bundles/dsb/docker-voting-bundle.dsb convert --stdout" "$KOMPOSE_ROOT/script/test/fixtures/bundles/dsb/output-k8s.json" "Service cannot be created because of missing port." convert::expect_success_and_warning "kompose --bundle $KOMPOSE_ROOT/script/test/fixtures/bundles/dsb/docker-voting-bundle.dsb convert --stdout" "$KOMPOSE_ROOT/script/test/fixtures/bundles/dsb/output-k8s.json" "Service cannot be created because of missing port."
###### ######
# Test related to restart options in docker-compose # Test related to restart options in docker-compose
# kubernetes test # kubernetes test
@ -113,4 +112,10 @@ convert::expect_success "kompose -f $KOMPOSE_ROOT/script/test/fixtures/restart-o
convert::expect_success "kompose -f $KOMPOSE_ROOT/script/test/fixtures/restart-options/docker-compose-restart-no.yml --provider openshift convert --stdout" "$KOMPOSE_ROOT/script/test/fixtures/restart-options/output-os-restart-no.json" convert::expect_success "kompose -f $KOMPOSE_ROOT/script/test/fixtures/restart-options/docker-compose-restart-no.yml --provider openshift convert --stdout" "$KOMPOSE_ROOT/script/test/fixtures/restart-options/output-os-restart-no.json"
convert::expect_success "kompose -f $KOMPOSE_ROOT/script/test/fixtures/restart-options/docker-compose-restart-onfail.yml --provider openshift convert --stdout" "$KOMPOSE_ROOT/script/test/fixtures/restart-options/output-os-restart-onfail.json" convert::expect_success "kompose -f $KOMPOSE_ROOT/script/test/fixtures/restart-options/docker-compose-restart-onfail.yml --provider openshift convert --stdout" "$KOMPOSE_ROOT/script/test/fixtures/restart-options/output-os-restart-onfail.json"
######
# Test key-only envrionment variable
export $(cat $KOMPOSE_ROOT/script/test/fixtures/keyonly-envs/envs)
convert::expect_success "kompose --file $KOMPOSE_ROOT/script/test/fixtures/keyonly-envs/env.yml convert --stdout" "$KOMPOSE_ROOT/script/test/fixtures/keyonly-envs/output-k8s.json"
unset $(cat $KOMPOSE_ROOT/script/test/fixtures/keyonly-envs/envs | cut -d'=' -f1)
exit $EXIT_STATUS exit $EXIT_STATUS

View File

@ -0,0 +1,25 @@
version: "2"
services:
redis-master:
image: gcr.io/google_containers/redis:e2e
ports:
- "6379"
redis-slave:
image: gcr.io/google_samples/gb-redisslave:v1
ports:
- "6379"
environment:
- GET_HOSTS_FROM=dns
- RACK_ENV=development
- SHOW=true
- SESSION_SECRET
frontend:
image: gcr.io/google-samples/gb-frontend:v4
ports:
- "80:80"
environment:
GET_HOSTS_FROM: dns
RACK_ENV: development
SHOW: 'true'
SESSION_SECRET:

View File

@ -0,0 +1 @@
SESSION_SECRET=session

View File

@ -0,0 +1,235 @@
{
"kind": "List",
"apiVersion": "v1",
"metadata": {},
"items": [
{
"kind": "Service",
"apiVersion": "v1",
"metadata": {
"name": "frontend",
"creationTimestamp": null,
"labels": {
"service": "frontend"
}
},
"spec": {
"ports": [
{
"name": "80",
"protocol": "TCP",
"port": 80,
"targetPort": 80
}
],
"selector": {
"service": "frontend"
}
},
"status": {
"loadBalancer": {}
}
},
{
"kind": "Service",
"apiVersion": "v1",
"metadata": {
"name": "redis-master",
"creationTimestamp": null,
"labels": {
"service": "redis-master"
}
},
"spec": {
"ports": [
{
"name": "6379",
"protocol": "TCP",
"port": 6379,
"targetPort": 6379
}
],
"selector": {
"service": "redis-master"
}
},
"status": {
"loadBalancer": {}
}
},
{
"kind": "Service",
"apiVersion": "v1",
"metadata": {
"name": "redis-slave",
"creationTimestamp": null,
"labels": {
"service": "redis-slave"
}
},
"spec": {
"ports": [
{
"name": "6379",
"protocol": "TCP",
"port": 6379,
"targetPort": 6379
}
],
"selector": {
"service": "redis-slave"
}
},
"status": {
"loadBalancer": {}
}
},
{
"kind": "Deployment",
"apiVersion": "extensions/v1beta1",
"metadata": {
"name": "frontend",
"creationTimestamp": null
},
"spec": {
"replicas": 1,
"template": {
"metadata": {
"creationTimestamp": null,
"labels": {
"service": "frontend"
}
},
"spec": {
"containers": [
{
"name": "frontend",
"image": "gcr.io/google-samples/gb-frontend:v4",
"ports": [
{
"containerPort": 80,
"protocol": "TCP"
}
],
"env": [
{
"name": "GET_HOSTS_FROM",
"value": "dns"
},
{
"name": "RACK_ENV",
"value": "development"
},
{
"name": "SESSION_SECRET",
"value": "session"
},
{
"name": "SHOW",
"value": "true"
}
],
"resources": {}
}
],
"restartPolicy": "Always"
}
},
"strategy": {}
},
"status": {}
},
{
"kind": "Deployment",
"apiVersion": "extensions/v1beta1",
"metadata": {
"name": "redis-master",
"creationTimestamp": null
},
"spec": {
"replicas": 1,
"template": {
"metadata": {
"creationTimestamp": null,
"labels": {
"service": "redis-master"
}
},
"spec": {
"containers": [
{
"name": "redis-master",
"image": "gcr.io/google_containers/redis:e2e",
"ports": [
{
"containerPort": 6379,
"protocol": "TCP"
}
],
"resources": {}
}
],
"restartPolicy": "Always"
}
},
"strategy": {}
},
"status": {}
},
{
"kind": "Deployment",
"apiVersion": "extensions/v1beta1",
"metadata": {
"name": "redis-slave",
"creationTimestamp": null
},
"spec": {
"replicas": 1,
"template": {
"metadata": {
"creationTimestamp": null,
"labels": {
"service": "redis-slave"
}
},
"spec": {
"containers": [
{
"name": "redis-slave",
"image": "gcr.io/google_samples/gb-redisslave:v1",
"ports": [
{
"containerPort": 6379,
"protocol": "TCP"
}
],
"env": [
{
"name": "GET_HOSTS_FROM",
"value": "dns"
},
{
"name": "RACK_ENV",
"value": "development"
},
{
"name": "SHOW",
"value": "true"
},
{
"name": "SESSION_SECRET",
"value": "session"
}
],
"resources": {}
}
],
"restartPolicy": "Always"
}
},
"strategy": {}
},
"status": {}
}
]
}