...

Source file src/github.com/Microsoft/hcsshim/internal/cni/registry_test.go

Documentation: github.com/Microsoft/hcsshim/internal/cni

     1  //go:build windows
     2  
     3  package cni
     4  
     5  import (
     6  	"testing"
     7  
     8  	"github.com/Microsoft/go-winio/pkg/guid"
     9  	"github.com/Microsoft/hcsshim/internal/regstate"
    10  )
    11  
    12  func newGUID(t *testing.T) guid.GUID {
    13  	t.Helper()
    14  	g, err := guid.NewV4()
    15  	if err != nil {
    16  		t.Fatal(err)
    17  	}
    18  	return g
    19  }
    20  
    21  func Test_LoadPersistedNamespaceConfig_NoConfig(t *testing.T) {
    22  	pnc, err := LoadPersistedNamespaceConfig(t.Name())
    23  	if pnc != nil {
    24  		t.Fatal("config should be nil")
    25  	}
    26  	if err == nil {
    27  		t.Fatal("err should be set")
    28  	} else {
    29  		if !regstate.IsNotFoundError(err) {
    30  			t.Fatal("err should be NotFoundError")
    31  		}
    32  	}
    33  }
    34  
    35  func Test_LoadPersistedNamespaceConfig_WithConfig(t *testing.T) {
    36  	pnc := NewPersistedNamespaceConfig(t.Name(), "test-container", newGUID(t))
    37  	if err := pnc.Store(); err != nil {
    38  		_ = pnc.Remove()
    39  		t.Fatalf("store failed with: %v", err)
    40  	}
    41  	defer func() {
    42  		_ = pnc.Remove()
    43  	}()
    44  
    45  	pnc2, err := LoadPersistedNamespaceConfig(t.Name())
    46  	if err != nil {
    47  		t.Fatal("should have no error on stored config")
    48  	}
    49  	if pnc2 == nil {
    50  		t.Fatal("stored config should have been returned")
    51  	} else {
    52  		if pnc.namespaceID != pnc2.namespaceID {
    53  			t.Fatal("actual/stored namespaceID not equal")
    54  		}
    55  		if pnc.ContainerID != pnc2.ContainerID {
    56  			t.Fatal("actual/stored ContainerID not equal")
    57  		}
    58  		if pnc.HostUniqueID != pnc2.HostUniqueID {
    59  			t.Fatal("actual/stored HostUniqueID not equal")
    60  		}
    61  		if !pnc2.stored {
    62  			t.Fatal("stored should be true for registry load")
    63  		}
    64  	}
    65  }
    66  
    67  func Test_PersistedNamespaceConfig_StoreNew(t *testing.T) {
    68  	pnc := NewPersistedNamespaceConfig(t.Name(), "test-container", newGUID(t))
    69  	if err := pnc.Store(); err != nil {
    70  		_ = pnc.Remove()
    71  		t.Fatalf("store failed with: %v", err)
    72  	}
    73  	defer func() {
    74  		_ = pnc.Remove()
    75  	}()
    76  }
    77  
    78  func Test_PersistedNamespaceConfig_StoreUpdate(t *testing.T) {
    79  	pnc := NewPersistedNamespaceConfig(t.Name(), "test-container", newGUID(t))
    80  	if err := pnc.Store(); err != nil {
    81  		_ = pnc.Remove()
    82  		t.Fatalf("store failed with: %v", err)
    83  	}
    84  	defer func() {
    85  		_ = pnc.Remove()
    86  	}()
    87  
    88  	pnc.ContainerID = "test-container2"
    89  	pnc.HostUniqueID = newGUID(t)
    90  	if err := pnc.Store(); err != nil {
    91  		_ = pnc.Remove()
    92  		t.Fatalf("store update failed with: %v", err)
    93  	}
    94  
    95  	// Verify the update
    96  	pnc2, err := LoadPersistedNamespaceConfig(t.Name())
    97  	if err != nil {
    98  		t.Fatal("stored config should have been returned")
    99  	}
   100  	if pnc.ContainerID != pnc2.ContainerID {
   101  		t.Fatal("actual/stored ContainerID not equal")
   102  	}
   103  	if pnc.HostUniqueID != pnc2.HostUniqueID {
   104  		t.Fatal("actual/stored HostUniqueID not equal")
   105  	}
   106  }
   107  
   108  func Test_PersistedNamespaceConfig_RemoveNotStored(t *testing.T) {
   109  	pnc := NewPersistedNamespaceConfig(t.Name(), "test-container", newGUID(t))
   110  	if err := pnc.Remove(); err != nil {
   111  		t.Fatalf("remove on not stored should not fail: %v", err)
   112  	}
   113  }
   114  
   115  func Test_PersistedNamespaceConfig_RemoveStoredKey(t *testing.T) {
   116  	pnc := NewPersistedNamespaceConfig(t.Name(), "test-container", newGUID(t))
   117  	if err := pnc.Store(); err != nil {
   118  		t.Fatalf("store failed with: %v", err)
   119  	}
   120  	if err := pnc.Remove(); err != nil {
   121  		t.Fatalf("remove on stored key should not fail: %v", err)
   122  	}
   123  }
   124  
   125  func Test_PersistedNamespaceConfig_RemovedOtherKey(t *testing.T) {
   126  	pnc := NewPersistedNamespaceConfig(t.Name(), "test-container", newGUID(t))
   127  	if err := pnc.Store(); err != nil {
   128  		t.Fatalf("store failed with: %v", err)
   129  	}
   130  
   131  	pnc2, err := LoadPersistedNamespaceConfig(t.Name())
   132  	if err != nil {
   133  		t.Fatal("should of found stored config")
   134  	}
   135  
   136  	if err := pnc.Remove(); err != nil {
   137  		t.Fatalf("remove on stored key should not fail: %v", err)
   138  	}
   139  
   140  	// Now remove the other key that has the invalid memory state
   141  	if err := pnc2.Remove(); err != nil {
   142  		t.Fatalf("remove on in-memory already removed should not fail: %v", err)
   143  	}
   144  }
   145  

View as plain text