knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/configmap/load_test.go (about)

     1  /*
     2  Copyright 2018 The Knative Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package configmap
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  	"path"
    23  	"testing"
    24  	"time"
    25  
    26  	"github.com/google/go-cmp/cmp"
    27  )
    28  
    29  func TestLoad(t *testing.T) {
    30  	want := map[string]string{
    31  		"foo":    "bar",
    32  		"a.b.c":  "blah",
    33  		".z.y.x": "hidden!",
    34  	}
    35  	tmpdir, err := os.MkdirTemp("", "")
    36  	if err != nil {
    37  		t.Fatal("TempDir() =", err)
    38  	}
    39  	defer os.RemoveAll(tmpdir)
    40  
    41  	// Kubernetes constructs ConfigMap Volumes in a strange way,
    42  	// this attempts to reflect that so that our testing is more
    43  	// representative of how this will function in the wild.
    44  	//  $/key             -> ..data/key
    45  	//  $/..data          -> {timestamp}
    46  	//  $/..{timestamp}/key == value
    47  
    48  	nowUnix := time.Now().Unix()
    49  	tsPart := fmt.Sprintf("..%d", nowUnix)
    50  	tsDir := path.Join(tmpdir, tsPart)
    51  	if err := os.Mkdir(tsDir, 0o755); err != nil {
    52  		t.Fatal("Mkdir() =", err)
    53  	}
    54  	dataLink := path.Join(tmpdir, "..data")
    55  	if err := os.Symlink(tsDir, dataLink); err != nil {
    56  		t.Fatal("Symlink() =", err)
    57  	}
    58  
    59  	// Write out the files as they should be loaded.
    60  	for k, v := range want {
    61  		// Write the actual file to $/.{timestamp}/key
    62  		if err := os.WriteFile(path.Join(tsDir, k), []byte(v), 0o644); err != nil {
    63  			t.Fatalf("WriteFile(..{ts}/%s) = %v", k, err)
    64  		}
    65  		// Symlink $/key => $/..data/key
    66  		if err := os.Symlink(path.Join(dataLink, k), path.Join(tmpdir, k)); err != nil {
    67  			t.Fatalf("Symlink(%s, ..data/%s) = %v", k, k, err)
    68  		}
    69  	}
    70  
    71  	got, err := Load(tmpdir)
    72  	if err != nil {
    73  		t.Fatal("Load() =", err)
    74  	}
    75  	if diff := cmp.Diff(want, got); diff != "" {
    76  		t.Error("Load (-want, +got) =", diff)
    77  	}
    78  }
    79  
    80  func TestLoadError(t *testing.T) {
    81  	if data, err := Load("/does/not/exist"); err == nil {
    82  		t.Errorf("Load() = %v, want error", data)
    83  	}
    84  }
    85  
    86  // TODO(#1401): When Prow runs as a mere mortal, uncomment this.
    87  // func TestReadFileError(t *testing.T) {
    88  // 	written := map[string]string{
    89  // 		"foo":    "bar",
    90  // 		"a.b.c":  "blah",
    91  // 		".z.y.x": "hidden!",
    92  // 	}
    93  // 	tmpdir, err := os.MkdirTemp("", "")
    94  // 	if err != nil {
    95  // 		t.Fatal("TempDir() =", err)
    96  // 	}
    97  // 	defer os.RemoveAll(tmpdir)
    98  
    99  // 	// Write out the files as write-only, so we fail reading.
   100  // 	for k, v := range written {
   101  // 		err := os.WriteFile(path.Join(tmpdir, k), []byte(v), 0200)
   102  // 		if err != nil {
   103  // 			t.Fatalf("WriteFile(%q) = %v", k, err)
   104  // 		}
   105  // 	}
   106  
   107  // 	if got, err := Load(tmpdir); err == nil {
   108  // 		t.Fatalf("Load() = %v, want error", got)
   109  // 	}
   110  // }
   111  
   112  func TestReadSymlinkedFileError(t *testing.T) {
   113  	tmpdir, err := os.MkdirTemp("", "")
   114  	if err != nil {
   115  		t.Fatal("TempDir() =", err)
   116  	}
   117  	defer os.RemoveAll(tmpdir)
   118  
   119  	if err := os.Symlink("not-found", path.Join(tmpdir, "foo")); err != nil {
   120  		t.Fatal("Symlink() =", err)
   121  	}
   122  
   123  	if got, err := Load(tmpdir); err == nil {
   124  		t.Fatalf("Load() = %v, want error", got)
   125  	}
   126  }