Skip to content

Commit

Permalink
extbldr: added mock subcommand with basic options
Browse files Browse the repository at this point in the history
added mock subcommand to build RPM from SRPMs. This internally calls the
mock tool to build the RPM in the architecture specified.
works with default config as of now. quiet option added for mock
  • Loading branch information
shyammohan-arista committed Sep 1, 2022
1 parent f531a48 commit 1f3a81b
Show file tree
Hide file tree
Showing 6 changed files with 233 additions and 2 deletions.
25 changes: 24 additions & 1 deletion .github/workflows/extbldr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ on: # yamllint disable-line rule:truthy
branches:
- 'main'

env:
TEST_TAG: extbldr:test

jobs:
gobuild:
name: Go lint and build
Expand Down Expand Up @@ -43,6 +46,7 @@ jobs:
- name: Run golint & go vet
run: |
cd extbldr
go fmt ./...
golint -set_exit_status ./...
go vet ./...
Expand All @@ -56,7 +60,7 @@ jobs:
- name: Setup buildx with docker-container driver
uses: docker/setup-buildx-action@v2

- name: Docker build
- name: Docker build and test privileged
uses: docker/build-push-action@v3
with:
# Dockerfile is within subdirectory
Expand All @@ -66,6 +70,25 @@ jobs:
secrets: |
GIT_AUTH_TOKEN=${{ secrets.GITHUB_TOKEN }}
load: true
tags: ${{ env.TEST_TAG }}
target: builder
- name: Test privileged
run: |
docker exec --privileged \
docker.io/library/${{ env.TEST_TAG }} sh -c 'cd src && \
go test ./... -tags=privileged'
- name: Dockerfile build without publish
uses: docker/build-push-action@v3
with:
# Dockerfile is within subdirectory
context: "{{defaultContext}}:extbldr"

# GIT_AUTH_TOKEN is unset for non-default context
secrets: |
GIT_AUTH_TOKEN=${{ secrets.GITHUB_TOKEN }}
# Publish is done by Arista docker-library to push to on-prem registry
# This just checks the docker build and can work off-prem.
target: publish
push: false
2 changes: 1 addition & 1 deletion extbldr/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ COPY cmd/ cmd/
COPY impl/ impl/
COPY util/ util/
COPY manifest/ manifest/
RUN go build -o /home/extbldr-robot/bin/extbldr && \
RUN go fmt ./... &&go build -o /home/extbldr-robot/bin/extbldr && \
go test ./... && \
GO111MODULE=off go get -u golang.org/x/lint/golint && \
PATH="$PATH:$HOME/go/bin" golint -set_exit_status ./... && \
Expand Down
54 changes: 54 additions & 0 deletions extbldr/cmd/mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) 2022 Arista Networks, Inc. All rights reserved.
// Arista Networks, Inc. Confidential and Proprietary.

package cmd

import (
// "fmt"

"github.com/spf13/cobra"
//"github.com/spf13/viper"

"extbldr/impl"
//"extbldr/util"
)

// var force bool
var arch string

// mockcmd represents the clone command
var mockcmd = &cobra.Command{
Use: "mock -t <Arch> -p <Package>",
Short: "Use mock to build the RPM from the SRPM",
Long: `The RPM for the specified architecture is built from the specified SRPM package in /bld/<Packagename>/SRPM and placed inside /bld/<Packagename>/RPM.
The local directory is <BASE_PATH>/<package>.
<BASE_PATH> is specified by the SrcDir configuration or the EXTBLDR_SRCDIR env var.`,
Args: func(cmd *cobra.Command, args []string) error {

//return fmt.Errorf("%s",viper.GetString("WorkingDir") )

return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
pkg, _ := cmd.Flags().GetString("package")
//basePath := viper.GetString("SrcDir")
err := impl.Mock(arch, pkg)
return err
},
}

func init() {
//mockcmd.Flags().BoolVarP(&force, "force", "f", false, "Clone again if the local directory already exists")
mockcmd.Flags().StringVarP(&arch, "target", "t", "", "target architecture for the rpm")
rootCmd.AddCommand(mockcmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// cloneCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// cloneCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
66 changes: 66 additions & 0 deletions extbldr/cmd/mock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) 2022 Arista Networks, Inc. All rights reserved.
// Arista Networks, Inc. Confidential and Proprietary.

//go:build privileged
// +build privileged

package cmd

import (
"github.com/stretchr/testify/assert"
//"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/spf13/viper"
)

func testMock(t *testing.T, workingDir string, pkgName string, quiet bool) {
viper.Set("WorkingDir", workingDir)
defer viper.Reset()
args := []string{"mock", "--target", "x86_64", "--package", pkgName}
rootCmd.SetArgs(args)

cmdErr := rootCmd.Execute()
assert.NoError(t, cmdErr)
}

func TestMock(t *testing.T) {
t.Log("Create temporary working directory")

workingDir, err := os.MkdirTemp("", "mock-test")
if err != nil {
t.Fatal(err)
}
srpmpath := filepath.Join(workingDir, "debugedit-1", "rpmbuild", "SRPMs")
err = os.MkdirAll(srpmpath, os.ModePerm)
if err != nil {
t.Fatal(err)
}

sampleSRPMPath := filepath.Join("testData", "mock-1", "debugedit-5.0-3.el9.src.rpm")
_, statErr := os.Stat(sampleSRPMPath)
if statErr != nil {
t.Fatal(statErr)
}

targetPath, absErr := filepath.Abs(sampleSRPMPath)
if absErr != nil {
t.Fatal(absErr)
}
linkPath := filepath.Join(srpmpath, "debugedit-5.0-3.el9.src.rpm")
symlinkErr := os.Symlink(targetPath, linkPath)
if symlinkErr != nil {
t.Fatal(symlinkErr)
}

defer os.RemoveAll(workingDir)
baseName := "debugedit-1"
t.Logf("WorkingDir: %s", workingDir)
t.Log("Test mock from SRPM")
testMock(t, workingDir, baseName, false)

t.Log("Test mock from SRPM quiet")
testMock(t, workingDir, baseName, true)
}
Binary file not shown.
88 changes: 88 additions & 0 deletions extbldr/impl/mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright (c) 2022 Arista Networks, Inc. All rights reserved.
// Arista Networks, Inc. Confidential and Proprietary.

package impl

import (
"extbldr/util"
"fmt"
"github.com/spf13/viper"
"os"
"path/filepath"
"regexp"
)

//var baseDir = "/var/extbldr"

// type can take SRPM or RPM
func getRpmFileNamesFromDir(dirPath string, rpmType string) ([]string, error) {
var rpmFileNames []string
files, err := os.ReadDir(dirPath)
if err != nil {
return rpmFileNames, fmt.Errorf("impl.Mock: os.ReadDir returned %v", err)
}

rpmRegex, _ := regexp.Compile("(?i).*[^(\\.src)]\\.rpm")
srpmRegex, _ := regexp.Compile("(?i).*\\.src\\.rpm")
var matchRegex *regexp.Regexp
if rpmType == "SRPM" {
matchRegex = srpmRegex
} else if rpmType == "RPM" {
matchRegex = rpmRegex
} else {
return rpmFileNames, fmt.Errorf("impl.Mock: unknown type: %s", rpmType)
}
var matched bool
//var err error
for _, file := range files {
matched = matchRegex.MatchString(file.Name())
if matched {
rpmFileNames = append(rpmFileNames, file.Name())
}
}
return rpmFileNames, nil
}

// Mock builds the RPM in specified architecture and
// copies it to /bld/pkg/RPM
func Mock(arch string, pkg string) error {
//srcDir := viper.GetString("SrcDir")
baseDir := viper.GetString("WorkingDir")
srpmPath := filepath.Join(baseDir, pkg, "rpmbuild", "SRPMs")
srpmName, srpmerr := getRpmFileNamesFromDir(srpmPath, "SRPM")
if srpmerr != nil {
return fmt.Errorf("impl.Mock: reading SRPMs errored out with %s", srpmerr)
}
srpmFullPath := filepath.Join(srpmPath, srpmName[0]) ////expecting single file

targetArg := "--target=" + arch

var mockErr error
if util.GlobalVar.Quiet {
mockErr = util.RunSystemCmd("mock", "--quiet", targetArg, srpmFullPath)
} else {
mockErr = util.RunSystemCmd("mock", targetArg, srpmFullPath)
}
if mockErr != nil {
return fmt.Errorf("impl.Mock:36: mock on %s to arch %s errored out with %s",
pkg, arch, mockErr)
}

rpmPath := filepath.Join(baseDir, pkg, "RPM")
creatErr := util.MaybeCreateDir("impl.mock", rpmPath)
if creatErr != nil {
return fmt.Errorf("impl.Mock: Creating %s errored out with %s", rpmPath, creatErr)
}

rpmFiles, rpmerr := getRpmFileNamesFromDir("/var/lib/mock/rocky+epel-9-x86_64/result/", "RPM")
if rpmerr != nil {
return fmt.Errorf("impl.Mock: reading RPMs errored out with %s", rpmerr)
}
for _, file := range rpmFiles {
mockErr = util.RunSystemCmd("mv", "-f", filepath.Join("/var/lib/mock/rocky+epel-9-x86_64/result/", file), rpmPath)
if mockErr != nil {
return fmt.Errorf("impl.Mock: move rpm file errored out with %s", mockErr)
}
}
return nil
}

0 comments on commit 1f3a81b

Please sign in to comment.