@@ -5,15 +5,24 @@ package oci
55
66import (
77 "bufio"
8+ "crypto/rand"
9+ "crypto/rsa"
10+ "crypto/x509"
11+ "crypto/x509/pkix"
12+ "encoding/pem"
813 "fmt"
14+ "math/big"
915 "net"
1016 "os/exec"
1117 "strconv"
1218 "strings"
1319 "syscall"
1420 "testing"
21+ "time"
1522
1623 "github.com/stretchr/testify/require"
24+
25+ loctest "sigs.k8s.io/kustomize/api/testutils/localizertest"
1726)
1827
1928func skipIfNoDocker (t * testing.T ) {
@@ -23,30 +32,56 @@ func skipIfNoDocker(t *testing.T) {
2332 }
2433}
2534
35+ // run calls Cmd.Run and wraps the error to include the output to make debugging
36+ // easier. Not safe for real code, but fine for tests.
37+ func run (cmd * exec.Cmd ) error {
38+ if out , err := cmd .CombinedOutput (); err != nil {
39+ return fmt .Errorf ("%w\n --- COMMAND OUTPUT ---\n %s" , err , string (out ))
40+ }
41+ return nil
42+ }
43+
2644// Set up the registry.
27- func registry (t * testing.T ) (* exec.Cmd , int , error ) {
45+ func registry (t * testing.T , certificate_path string , key_path string ) (* exec.Cmd , int , error ) {
2846 skipIfNoDocker (t )
47+ t .Helper ()
48+
49+ const container_cert_path = "/certs/cert.pem"
50+ const container_key_path = "/certs/key.pem"
2951
30- container_name := fmt . Sprintf ( "%s_%d" , t .Name (), 15 )
52+ container_name := t .Name ()
3153 internal_port := 5000
3254
33- registry := exec .Command ("docker" , "run " ,
55+ create := exec .Command ("docker" , "create " ,
3456 "--rm" ,
35- "-p" , fmt .Sprintf ("0:%d" , internal_port ),
3657 "--name" , container_name ,
58+ "--publish" , fmt .Sprintf ("0:%d" , internal_port ),
59+ "--env" , "REGISTRY_HTTP_TLS_CERTIFICATE=" + container_cert_path ,
60+ "--env" , "REGISTRY_HTTP_TLS_KEY=" + container_key_path ,
3761 "docker.io/library/registry:3.0.0" ,
3862 )
63+ require .NoError (t , run (create ))
64+
65+ cert_upload := exec .Command ("docker" , "cp" , certificate_path , container_name + ":" + container_cert_path )
66+ require .NoError (t , run (cert_upload ))
67+ key_upload := exec .Command ("docker" , "cp" , key_path , container_name + ":" + container_key_path )
68+ require .NoError (t , run (key_upload ))
3969
40- stdout , err := registry .StderrPipe ()
70+ start := exec .Command ("docker" , "start" ,
71+ "--attach" , "--interactive" ,
72+ container_name ,
73+ )
74+
75+ stdout , err := start .StderrPipe ()
4176 if err != nil {
4277 t .Fatal (err )
4378 }
4479
45- if err := registry .Start (); err != nil {
80+ if err := start .Start (); err != nil {
4681 t .Fatal (err )
4782 }
4883
49- t .Cleanup (func () { registry .Process .Signal (syscall .SIGTERM ) })
84+ t .Cleanup (func () { start .Process .Signal (syscall .SIGTERM ) })
5085
5186 scanner := bufio .NewScanner (stdout )
5287 for scanner .Scan () {
@@ -75,16 +110,71 @@ func registry(t *testing.T) (*exec.Cmd, int, error) {
75110 t .Fatal (err )
76111 }
77112
78- return registry , port , nil
113+ return start , port , nil
114+ }
115+
116+ func generateSelfSignedCert (t * testing.T ) (certificate string , key string ) {
117+ t .Helper ()
118+
119+ // Generate private key
120+ privateKey , err := rsa .GenerateKey (rand .Reader , 1024 )
121+ if err != nil {
122+ t .Fatalf ("failed to generate private key: %v" , err )
123+ }
124+
125+ // Certificate template
126+ tmpl := x509.Certificate {
127+ SerialNumber : big .NewInt (1 ),
128+ Subject : pkix.Name {
129+ CommonName : "localhost" ,
130+ },
131+ NotBefore : time .Now (),
132+ NotAfter : time .Now ().Add (24 * time .Hour ),
133+
134+ KeyUsage : x509 .KeyUsageKeyEncipherment | x509 .KeyUsageDigitalSignature ,
135+ ExtKeyUsage : []x509.ExtKeyUsage {x509 .ExtKeyUsageServerAuth },
136+ BasicConstraintsValid : true ,
137+ }
138+
139+ // Self-sign the certificate
140+ derBytes , err := x509 .CreateCertificate (rand .Reader , & tmpl , & tmpl , & privateKey .PublicKey , privateKey )
141+ if err != nil {
142+ t .Fatalf ("failed to create certificate: %v" , err )
143+ }
144+
145+ return string (pem .EncodeToMemory (& pem.Block {
146+ Type : "CERTIFICATE" ,
147+ Bytes : derBytes ,
148+ })),
149+ string (pem .EncodeToMemory (& pem.Block {
150+ Type : "RSA PRIVATE KEY" ,
151+ Bytes : x509 .MarshalPKCS1PrivateKey (privateKey ),
152+ }))
79153}
80154
81155func TestFnContainerTransformerWithConfig (t * testing.T ) {
82- registry , port , err := registry (t )
156+ certificate , key := generateSelfSignedCert (t )
157+ cert_path := "certs/cert.pem"
158+ key_path := "certs/key.pem"
159+
160+ kustomization := map [string ]string {
161+ "src/README.md" : `# NO VALID FILE
162+ ` ,
163+ cert_path : certificate ,
164+ key_path : key ,
165+ }
166+ // clock := NewFakePassiveClock(time.Date(int(2025), time.July, int(28), int(20), int(56), int(0), int(0), time.UTC))
167+
168+ _ , _ , target := loctest .PrepareFs (t , []string {"src" , "certs" }, kustomization )
169+ loctest .SetWorkingDir (t , target .Join ("src" ))
170+
171+ registry , port , err := registry (t , target .Join (cert_path ), target .Join (key_path ))
83172 require .NoError (t , err )
173+
84174 // t.Cleanup(func() {registry.})
85175 require .NotNil (t , registry )
86176 t .Setenv ("asdfsd" , "asdfadsf" )
87177
88- require .Equal (t , port , 5 )
178+ require .Equal (t , port , 7 )
89179
90180}
0 commit comments