1
1
package devcontainer_test
2
2
3
3
import (
4
+ "crypto/md5"
4
5
"fmt"
5
6
"io"
6
7
"net/url"
@@ -11,6 +12,7 @@ import (
11
12
12
13
"github.com/coder/envbuilder"
13
14
"github.com/coder/envbuilder/devcontainer"
15
+ "github.com/coder/envbuilder/devcontainer/features"
14
16
"github.com/coder/envbuilder/registrytest"
15
17
"github.com/go-git/go-billy/v5/memfs"
16
18
"github.com/google/go-containerregistry/pkg/name"
@@ -36,6 +38,72 @@ func TestParse(t *testing.T) {
36
38
require .Equal (t , "Dockerfile" , parsed .Build .Dockerfile )
37
39
}
38
40
41
+ func TestCompileWithFeatures (t * testing.T ) {
42
+ t .Parallel ()
43
+ registry := registrytest .New (t )
44
+ featureOne := registrytest .WriteContainer (t , registry , "coder/test:tomato" , features .TarLayerMediaType , map [string ]any {
45
+ "install.sh" : "hey" ,
46
+ "devcontainer-feature.json" : features.Spec {
47
+ ID : "rust" ,
48
+ Version : "tomato" ,
49
+ Name : "Rust" ,
50
+ Description : "Example description!" ,
51
+ ContainerEnv : map [string ]string {
52
+ "TOMATO" : "example" ,
53
+ },
54
+ },
55
+ })
56
+ featureTwo := registrytest .WriteContainer (t , registry , "coder/test:potato" , features .TarLayerMediaType , map [string ]any {
57
+ "install.sh" : "hey" ,
58
+ "devcontainer-feature.json" : features.Spec {
59
+ ID : "go" ,
60
+ Version : "potato" ,
61
+ Name : "Go" ,
62
+ Description : "Example description!" ,
63
+ ContainerEnv : map [string ]string {
64
+ "POTATO" : "example" ,
65
+ },
66
+ },
67
+ })
68
+ // Update the tag to ensure it comes from the feature value!
69
+ featureTwoFake := strings .Join (append (strings .Split (featureTwo , ":" )[:2 ], "faketag" ), ":" )
70
+
71
+ raw := `{
72
+ "build": {
73
+ "dockerfile": "Dockerfile",
74
+ "context": ".",
75
+ },
76
+ // Comments here!
77
+ "image": "codercom/code-server:latest",
78
+ "features": {
79
+ "` + featureOne + `": {},
80
+ "` + featureTwoFake + `": "potato"
81
+ }
82
+ }`
83
+ dc , err := devcontainer .Parse ([]byte (raw ))
84
+ require .NoError (t , err )
85
+ fs := memfs .New ()
86
+ params , err := dc .Compile (fs , "" , envbuilder .MagicDir , "" )
87
+ require .NoError (t , err )
88
+
89
+ // We have to SHA because we get a different MD5 every time!
90
+ featureOneMD5 := md5 .Sum ([]byte (featureOne ))
91
+ featureOneSha := fmt .Sprintf ("%x" , featureOneMD5 [:4 ])
92
+ featureTwoMD5 := md5 .Sum ([]byte (featureTwo ))
93
+ featureTwoSha := fmt .Sprintf ("%x" , featureTwoMD5 [:4 ])
94
+
95
+ require .Equal (t , `FROM codercom/code-server:latest
96
+
97
+ USER root
98
+ # Go potato - Example description!
99
+ ENV POTATO=example
100
+ RUN .envbuilder/features/test-` + featureTwoSha + `/install.sh
101
+ # Rust tomato - Example description!
102
+ ENV TOMATO=example
103
+ RUN .envbuilder/features/test-` + featureOneSha + `/install.sh
104
+ USER 1000` , params .DockerfileContent )
105
+ }
106
+
39
107
func TestCompileDevContainer (t * testing.T ) {
40
108
t .Parallel ()
41
109
t .Run ("WithImage" , func (t * testing.T ) {
0 commit comments