Repository: threefoldtech/zos (go)

Issue:
zdb decomission tries to delete volume names /etc/resolv.conf
```
[+] provisiond: 2019-11-27T11:23:55Z info reservation expired created=2019-11-27T12:13:51+01:00 duration=10m0s id=140-1 type=zdb
[+] provisiond: 2019-11-27T11:23:55Z info start decommissioning reservation duration=10m0s id=140-1 tag="source: FSStore" type=zdb
[+] provisiond: 2019-11-27T11:23:55Z debug connect to 0-db and delete namespace addr=unix:///var/run/zdb_zdbedafef37-ac5a-4ceb-ba73-05d805cf4bba/zdb.sock namespace=140-1
[+] provisiond: 2019-11-27T11:23:55Z error failed to decommission reservation 140-1 error="decommissioning of reservation failed: failed to reclaim storage on volume resolv.conf: volume named resolv.conf not found"
[+] provisiond: 2019-11-27T11:23:55Z error failed to decommission reservation 140-1 error="decommissioning of reservation failed: failed to reclaim storage on volume resolv.conf: volume named resolv.conf not found"
```

Pull request description:
provisiond: improve way to get data volume during zdb decomission
fixed #413

Patch:
diff --git a/pkg/provision/zdb.go b/pkg/provision/zdb.go
index a87cfb30..4563822d 100644
--- a/pkg/provision/zdb.go
+++ b/pkg/provision/zdb.go
@@ -384,7 +384,10 @@ func zdbDecommission(ctx context.Context, reservation *Reservation) error {
 	if len(c.Mounts) < 1 {
 		return fmt.Errorf("no mountpoint find in 0-db container, cannot reclaim storage")
 	}
-	volume := filepath.Base(c.Mounts[0].Source)
+	volume, err := findDataVolume(c.Mounts)
+	if err != nil {
+		return err
+	}
 
 	if err := storage.Claim(volume, config.Size); err != nil {
 		return errors.Wrapf(err, "failed to reclaim storage on volume %s", volume)
@@ -413,3 +416,12 @@ func isPublic(ip net.IP) bool {
 		ip.IsLinkLocalMulticast() ||
 		ip.IsInterfaceLocalMulticast())
 }
+
+func findDataVolume(mounts []pkg.MountInfo) (string, error) {
+	for _, mount := range mounts {
+		if mount.Target == "/data" {
+			return filepath.Base(mount.Source), nil
+		}
+	}
+	return "", fmt.Errorf("data volume not found")
+}


Test patch:
diff --git a/pkg/provision/zdb_test.go b/pkg/provision/zdb_test.go
index 20e9e745..4830ab40 100644
--- a/pkg/provision/zdb_test.go
+++ b/pkg/provision/zdb_test.go
@@ -7,6 +7,7 @@ import (
 	"strings"
 	"testing"
 
+	"github.com/stretchr/testify/assert"
 	"github.com/stretchr/testify/require"
 	"github.com/threefoldtech/zbus"
 	"github.com/threefoldtech/zos/pkg"
@@ -209,3 +210,64 @@ func TestZDBProvisionNoMappingContainerDoesNotExists(t *testing.T) {
 	require.Error(err)
 	require.True(strings.Contains(err.Error(), "/var/run/zdb_container-id"))
 }
+
+func Test_findDataVolume(t *testing.T) {
+	type args struct {
+		mounts []pkg.MountInfo
+	}
+	tests := []struct {
+		name    string
+		args    args
+		want    string
+		wantErr bool
+	}{
+		{
+			name: "exists",
+			args: args{
+				mounts: []pkg.MountInfo{
+					{
+						Target: "/data",
+						Source: "/mnt/sda/subovl1",
+					},
+					{
+						Target: "/etc/resolv.conf",
+						Source: "/etc/resolv.conf",
+					},
+				},
+			},
+			want:    "subovl1",
+			wantErr: false,
+		},
+		{
+			name: "non-exists",
+			args: args{
+				mounts: []pkg.MountInfo{
+					{
+						Target: "/etc/resolv.conf",
+						Source: "/etc/resolv.conf",
+					},
+				},
+			},
+			want:    "",
+			wantErr: true,
+		},
+		{
+			name: "empty",
+			args: args{
+				mounts: []pkg.MountInfo{},
+			},
+			want:    "",
+			wantErr: true,
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			got, err := findDataVolume(tt.args.mounts)
+			if tt.wantErr {
+				require.Error(t, err)
+			} else {
+				assert.Equal(t, tt.want, got)
+			}
+		})
+	}
+}