1
1
//
2
- // Copyright 2022 Red Hat, Inc.
2
+ // Copyright 2022-2023 Red Hat, Inc.
3
3
//
4
4
// Licensed under the Apache License, Version 2.0 (the "License");
5
5
// you may not use this file except in compliance with the License.
16
16
package generator
17
17
18
18
import (
19
+ "errors"
19
20
"fmt"
20
21
"reflect"
21
22
"strings"
@@ -24,10 +25,13 @@ import (
24
25
"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
25
26
v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
26
27
"github.com/devfile/api/v2/pkg/attributes"
28
+ "github.com/devfile/library/v2/pkg/devfile"
27
29
"github.com/devfile/library/v2/pkg/devfile/parser"
30
+ context "github.com/devfile/library/v2/pkg/devfile/parser/context"
28
31
"github.com/devfile/library/v2/pkg/devfile/parser/data"
29
32
"github.com/devfile/library/v2/pkg/devfile/parser/data/v2/common"
30
33
"github.com/devfile/library/v2/pkg/testingutil"
34
+ "github.com/devfile/library/v2/pkg/testingutil/filesystem"
31
35
"github.com/devfile/library/v2/pkg/util"
32
36
"github.com/golang/mock/gomock"
33
37
"github.com/google/go-cmp/cmp"
@@ -1345,6 +1349,53 @@ func TestGetPodTemplateSpec(t *testing.T) {
1345
1349
},
1346
1350
wantErr : true ,
1347
1351
},
1352
+ {
1353
+ name : "GetContainers returns err" ,
1354
+ args : args {
1355
+ devfileObj : func (ctrl * gomock.Controller ) parser.DevfileObj {
1356
+ mockDevfileData := data .NewMockDevfileData (ctrl )
1357
+ mockDevfileData .EXPECT ().GetComponents (gomock .Any ()).Return (nil , errors .New ("an error" )).AnyTimes ()
1358
+ return parser.DevfileObj {
1359
+ Data : mockDevfileData ,
1360
+ }
1361
+ },
1362
+ },
1363
+ wantErr : true ,
1364
+ },
1365
+ {
1366
+ name : "GetDevfileContainerComponents returns err" ,
1367
+ args : args {
1368
+ devfileObj : func (ctrl * gomock.Controller ) parser.DevfileObj {
1369
+ containers := []v1alpha2.Component {
1370
+ {
1371
+ Name : "main" ,
1372
+ ComponentUnion : v1.ComponentUnion {
1373
+ Container : & v1.ContainerComponent {
1374
+ Container : v1.Container {
1375
+ Image : "an-image" ,
1376
+ },
1377
+ },
1378
+ },
1379
+ Attributes : attributes.Attributes {
1380
+ ContainerOverridesAttribute : apiext.JSON {Raw : []byte ("{\" spec\" : \" serviceAccountName\" : \" new-service-account\" }" )},
1381
+ },
1382
+ },
1383
+ }
1384
+ events := v1alpha2.Events {}
1385
+ mockDevfileData := data .NewMockDevfileData (ctrl )
1386
+ mockDevfileData .EXPECT ().GetComponents (gomock .Any ()).Return (containers , nil ).AnyTimes ()
1387
+ mockDevfileData .EXPECT ().GetProjects (gomock .Any ()).Return (nil , nil ).AnyTimes ()
1388
+ mockDevfileData .EXPECT ().GetEvents ().Return (events ).AnyTimes ()
1389
+ mockDevfileData .EXPECT ().GetDevfileContainerComponents (gomock .Any ()).Return (nil , errors .New ("an error" )).AnyTimes ()
1390
+ mockDevfileData .EXPECT ().GetAttributes ().Return (attributes.Attributes {}, nil )
1391
+ mockDevfileData .EXPECT ().GetSchemaVersion ().Return ("2.1.0" ).AnyTimes ()
1392
+ return parser.DevfileObj {
1393
+ Data : mockDevfileData ,
1394
+ }
1395
+ },
1396
+ },
1397
+ wantErr : true ,
1398
+ },
1348
1399
{
1349
1400
name : "Devfile with local container-override on the first container only" ,
1350
1401
args : args {
@@ -1833,6 +1884,68 @@ func TestGetPodTemplateSpec(t *testing.T) {
1833
1884
},
1834
1885
},
1835
1886
},
1887
+ {
1888
+ name : "Filter components by name" ,
1889
+ args : args {
1890
+ devfileObj : func (ctrl * gomock.Controller ) parser.DevfileObj {
1891
+ containers := []v1alpha2.Component {
1892
+ {
1893
+ Name : "main" ,
1894
+ ComponentUnion : v1.ComponentUnion {
1895
+ Container : & v1.ContainerComponent {
1896
+ Container : v1.Container {
1897
+ Image : "an-image" ,
1898
+ },
1899
+ },
1900
+ },
1901
+ },
1902
+ {
1903
+ Name : "tools" ,
1904
+ ComponentUnion : v1.ComponentUnion {
1905
+ Container : & v1.ContainerComponent {
1906
+ Container : v1.Container {
1907
+ Image : "a-tool-image" ,
1908
+ },
1909
+ },
1910
+ },
1911
+ },
1912
+ }
1913
+ parserArgs := parser.ParserArgs {
1914
+ Data : []byte (`schemaVersion: 2.2.0` ),
1915
+ }
1916
+ var err error
1917
+ devfile , _ , err := devfile .ParseDevfileAndValidate (parserArgs )
1918
+ if err != nil {
1919
+ t .Errorf ("error creating devfile: %v" , err )
1920
+ }
1921
+ devfile .Ctx = context .FakeContext (filesystem .NewFakeFs (), "/devfile.yaml" )
1922
+ devfile .Data .AddComponents (containers )
1923
+ return devfile
1924
+ },
1925
+ podTemplateParams : PodTemplateParams {
1926
+ Options : common.DevfileOptions {
1927
+ FilterByName : "tools" ,
1928
+ },
1929
+ },
1930
+ },
1931
+ want : & corev1.PodTemplateSpec {
1932
+ Spec : corev1.PodSpec {
1933
+ Containers : []corev1.Container {
1934
+ {
1935
+ Name : "tools" ,
1936
+ Image : "a-tool-image" ,
1937
+ Env : []corev1.EnvVar {
1938
+ {Name : "PROJECTS_ROOT" , Value : "/projects" },
1939
+ {Name : "PROJECT_SOURCE" , Value : "/projects" },
1940
+ },
1941
+ ImagePullPolicy : corev1 .PullAlways ,
1942
+ Ports : []corev1.ContainerPort {},
1943
+ },
1944
+ },
1945
+ InitContainers : []corev1.Container {},
1946
+ },
1947
+ },
1948
+ },
1836
1949
}
1837
1950
for _ , tt := range tests {
1838
1951
t .Run (tt .name , func (t * testing.T ) {
0 commit comments