Skip to content

Commit

Permalink
isolator: fixed isolator, added test
Browse files Browse the repository at this point in the history
`acbuild isolator add ...` was broken, producing the error:

```
isolator add: json: error calling MarshalJSON for type *types.App: invalid isolator
```

This commit fixes this, and adds a test for isolator add to be able to
catch this type of regression in the future.
  • Loading branch information
Derek Gonyeo committed Aug 2, 2016
1 parent 83d6ca7 commit 0d8f8e0
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 5 deletions.
26 changes: 21 additions & 5 deletions lib/isolator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package lib

import (
"encoding/json"
"fmt"

"github.com/appc/acbuild/util"

Expand Down Expand Up @@ -64,12 +65,27 @@ func (a *ACBuild) AddIsolator(name string, value []byte) (err error) {
if s.App == nil {
s.App = newManifestApp()
}
_, ok := types.ResourceIsolatorNames[*acid]
if !ok {
_, ok = types.LinuxIsolatorNames[*acid]
if !ok {
return fmt.Errorf("unknown isolator name: %s", name)
}
}
i := &types.Isolator{
Name: *acid,
ValueRaw: &rawMsg,
}
blob, err := json.Marshal(i)
if err != nil {
return err
}
err = i.UnmarshalJSON(blob)
if err != nil {
return err
}
removeIsolatorFromMan(*acid)(s)
s.App.Isolators = append(s.App.Isolators,
types.Isolator{
Name: *acid,
ValueRaw: &rawMsg,
})
s.App.Isolators = append(s.App.Isolators, *i)
return nil
}
return util.ModifyManifest(fn, a.CurrentACIPath)
Expand Down
74 changes: 74 additions & 0 deletions tests/isolator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2016 The appc Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package tests

import (
"encoding/json"
"io/ioutil"
"os"
"testing"

"github.com/appc/spec/schema"
"github.com/appc/spec/schema/types"
)

const (
isolatorName = "os/linux/capabilities-retain-set"
isolatorValue = `{ "set": ["CAP_NET_BIND_SERVICE"] }`
)

func manWithIsolators(isolators types.Isolators) schema.ImageManifest {
man := emptyManifestWithApp()
man.App.Isolators = isolators
return man
}

func TestAddIsolator(t *testing.T) {
workingDir := setUpTest(t)
defer cleanUpTest(workingDir)

isoFile, err := ioutil.TempFile("", "acbuild-test")
if err != nil {
t.Fatalf("%v\n", err)
}
_, err = isoFile.Write([]byte(isolatorValue))
isoFile.Close()
if err != nil {
t.Fatalf("%v\n", err)
}
defer os.RemoveAll(isoFile.Name())

err = runACBuildNoHist(workingDir, "isolator", "add", isolatorName, isoFile.Name())
if err != nil {
t.Fatalf("%v\n", err)
}

valueBlob := json.RawMessage(isolatorValue)
i := &types.Isolator{
Name: *types.MustACIdentifier(isolatorName),
ValueRaw: &valueBlob,
}
blob, err := json.Marshal(i)
if err != nil {
t.Fatalf("%v\n", err)
}
err = i.UnmarshalJSON(blob)
if err != nil {
t.Fatalf("%v\n", err)
}

checkManifest(t, workingDir, manWithIsolators(types.Isolators{*i}))
checkEmptyRootfs(t, workingDir)
}

0 comments on commit 0d8f8e0

Please sign in to comment.