|  | 
|  | 1 | +/* | 
|  | 2 | +   Copyright The containerd 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 namestore | 
|  | 18 | + | 
|  | 19 | +import ( | 
|  | 20 | +	"os" | 
|  | 21 | +	"path/filepath" | 
|  | 22 | +	"testing" | 
|  | 23 | + | 
|  | 24 | +	"gotest.tools/v3/assert" | 
|  | 25 | + | 
|  | 26 | +	"github.com/containerd/nerdctl/v2/pkg/store" | 
|  | 27 | +) | 
|  | 28 | + | 
|  | 29 | +func TestNamestoreNew(t *testing.T) { | 
|  | 30 | +	tempDir := t.TempDir() | 
|  | 31 | + | 
|  | 32 | +	tests := []struct { | 
|  | 33 | +		name      string | 
|  | 34 | +		namespace string | 
|  | 35 | +		wantErr   bool | 
|  | 36 | +		errChecks []error | 
|  | 37 | +	}{ | 
|  | 38 | +		{ | 
|  | 39 | +			name:      "empty namespace", | 
|  | 40 | +			namespace: "", | 
|  | 41 | +			wantErr:   true, | 
|  | 42 | +			errChecks: []error{ErrNameStore, store.ErrInvalidArgument}, | 
|  | 43 | +		}, | 
|  | 44 | +		{ | 
|  | 45 | +			name:      "valid namespace", | 
|  | 46 | +			namespace: "testnamespace", | 
|  | 47 | +			wantErr:   false, | 
|  | 48 | +		}, | 
|  | 49 | +	} | 
|  | 50 | + | 
|  | 51 | +	for _, tt := range tests { | 
|  | 52 | +		t.Run(tt.name, func(t *testing.T) { | 
|  | 53 | +			ns, err := New(tempDir, tt.namespace) | 
|  | 54 | +			if tt.wantErr { | 
|  | 55 | +				assert.Assert(t, err != nil, "New should return an error for %s", tt.name) | 
|  | 56 | +				for _, errCheck := range tt.errChecks { | 
|  | 57 | +					assert.ErrorIs(t, err, errCheck, "Error should contain %v for %s", errCheck, tt.name) | 
|  | 58 | +				} | 
|  | 59 | +			} else { | 
|  | 60 | +				assert.NilError(t, err, "New should succeed for %s", tt.name) | 
|  | 61 | +				assert.Assert(t, ns != nil, "New should return a non-nil NameStore for %s", tt.name) | 
|  | 62 | + | 
|  | 63 | +				// Check that the directory is created in the correct path | 
|  | 64 | +				expectedDir := filepath.Join(tempDir, "names", tt.namespace) | 
|  | 65 | +				_, err = os.Stat(expectedDir) | 
|  | 66 | +				assert.NilError(t, err, "Directory should be created at the correct path for %s", tt.name) | 
|  | 67 | +			} | 
|  | 68 | +		}) | 
|  | 69 | +	} | 
|  | 70 | +} | 
0 commit comments