Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crit: fix proto imports for library #109

Merged
merged 4 commits into from
May 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,21 @@ jobs:
run: sudo -E make test

- name: Test magicgen script
run: sudo -E make -C scripts test
run: sudo -E make -C scripts/magic-gen test

- name: Test CRIT
run: |
if [ "${{ matrix.criu_branch }}" = "criu-dev" ]; then
# We need to use the protobuf definitions from the criu-dev
# branch as it might have changed.
make -C crit update-proto GIT_BRANCH=${{ matrix.criu_branch }}
# First update protobuf. It is too old in the Ubuntu image.
curl -Lo protoc.zip https://github.com/protocolbuffers/protobuf/releases/download/v3.20.3/protoc-3.20.3-linux-x86_64.zip
curl -Lo protoc.zip https://github.com/protocolbuffers/protobuf/releases/download/v22.2/protoc-22.2-linux-x86_64.zip
sudo unzip -o protoc.zip -d /usr
sudo -E make -C crit clean
make -C crit gen-proto
sudo -E make -C crit all
# We need to use the protobuf definitions from the criu-dev
# branch as it might have changed.
sudo -E make -C scripts/proto-gen proto-update GIT_BRANCH=${{ matrix.criu_branch }}
# Generate the bindings
sudo -E make -C scripts/proto-gen
# Build the CRIT binary
sudo -E make -C crit clean bin/crit
fi
sudo -E make -C crit unit-test
sudo -E make -C test crit-test
Expand Down
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ test/crit/test-imgs
test/crit/crit-test.coverage
test/.coverage/
image
scripts/*.h
scripts/expected.go
scripts/output.go
scripts/magic-gen/*.h
scripts/magic-gen/expected.go
scripts/magic-gen/output.go
crit/bin
crit/test-imgs/
42 changes: 3 additions & 39 deletions crit/Makefile
Original file line number Diff line number Diff line change
@@ -1,43 +1,7 @@
GO ?= go
CRIU ?= criu

# The import path that protoc will use if a proto file imports another one
import_path := github.com/checkpoint-restore/go-criu/crit/images
# Path to .proto source files
proto_path := ./images
# Generate string of all .proto filenames
proto_files := $(sort $(subst $(proto_path)/,,$(wildcard $(proto_path)/*.proto)))
# Generate M flag to specify import path for all .proto files
# and replace all spaces with commas to use with go_opt flag
comma := ,
proto_opts := $(subst $() $(),$(comma),$(patsubst %,M%=$(import_path),$(proto_files)))
GIT_BRANCH := master

all: gen-proto bin/crit

update-proto:
rm ./images/*.proto || true
git clone --depth 1 --branch $(GIT_BRANCH) https://github.com/checkpoint-restore/criu criu-temp
cp criu-temp/images/*.proto ./images/
# rpc.proto is not an image and it is used only to communicate criu-service and swrk.
rm -rf criu-temp images/rpc.proto
# To prevent namespace conflict with proto files
# in github.com/letsencrypt/boulder, we prepend
# a prefix to the filenames.
mv ./images/sa.proto ./images/criu-sa.proto
sed -i 's/sa\.proto/criu-sa\.proto/g' images/*.proto
mv ./images/core.proto ./images/criu-core.proto
sed -i 's/core\.proto/criu-core\.proto/g' images/*.proto

gen-proto:
rm -f ./images/*.pb.go
@protoc \
--proto_path=$(proto_path) \
--go_out=$(proto_path) \
--go_opt=paths=source_relative,$(proto_opts) \
$(proto_files)

bin/crit: cmd/cli.go
bin/crit: cmd/main.go
$(GO) build ${GOFLAGS} -o $@ $^

../test/loop/loop:
Expand All @@ -52,10 +16,10 @@ test-imgs: ../test/loop/loop

unit-test: test-imgs
$(eval GOFLAGS ?= -cover)
go test ${GOFLAGS} -v ./...
$(GO) test ${GOFLAGS} -v ./...

clean:
@rm -f bin/crit
@rm -rf test-imgs

.PHONY: all gen-proto update-proto unit-test clean
.PHONY: unit-test clean
132 changes: 105 additions & 27 deletions crit/cmd/cli.go → crit/cli/cli.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cli

import (
"encoding/json"
Expand All @@ -13,7 +13,7 @@

var (
// The crit service used to invoke all commands
c crit.CritSvc
c crit.Critter

// All members needed for crit struct
inputFilePath string
Expand All @@ -40,11 +40,29 @@
Long: `Convert the input binary image to JSON and write it to a file.
If no output file is provided, the JSON is printed to stdout.`,
Run: func(cmd *cobra.Command, args []string) {
c = crit.NewCli(inputFilePath, outputFilePath,
var (
inputFile *os.File
err error
)
if inputFilePath == "" {
inputFile = os.Stdin
} else {
inputFile, err = os.Open(inputFilePath)
if err != nil {
log.Fatal(fmt.Errorf("error opening input file: %w", err))
}

Check warning on line 53 in crit/cli/cli.go

View check run for this annotation

Codecov / codecov/patch

crit/cli/cli.go#L52-L53

Added lines #L52 - L53 were not covered by tests
defer inputFile.Close()
}

c = crit.New(inputFile, nil,
inputDirPath, pretty, noPayload)
img, err := c.Decode()
entryType, err := GetEntryTypeFromImg(inputFile)
if err != nil {
log.Fatal(err)
log.Fatal(fmt.Errorf("error getting protobuf binding: %w", err))
}
img, err := c.Decode(entryType)
if err != nil {
log.Fatal(fmt.Errorf("error decoding image: %w", err))

Check warning on line 65 in crit/cli/cli.go

View check run for this annotation

Codecov / codecov/patch

crit/cli/cli.go#L65

Added line #L65 was not covered by tests
}

var jsonData []byte
Expand All @@ -54,7 +72,7 @@
jsonData, err = json.Marshal(img)
}
if err != nil {
log.Fatal(errors.New(fmt.Sprint("Error processing data into JSON: ", err)))
log.Fatal(fmt.Errorf("error processing data into JSON: %w", err))

Check warning on line 75 in crit/cli/cli.go

View check run for this annotation

Codecov / codecov/patch

crit/cli/cli.go#L75

Added line #L75 was not covered by tests
}
// If no output file, print to stdout
if outputFilePath == "" {
Expand All @@ -64,12 +82,13 @@
// Write to output file
jsonFile, err := os.Create(outputFilePath)
if err != nil {
log.Fatal(errors.New(fmt.Sprint("Error opening destination file: ", err)))
log.Fatal(fmt.Errorf("error opening destination file: %w", err))

Check warning on line 85 in crit/cli/cli.go

View check run for this annotation

Codecov / codecov/patch

crit/cli/cli.go#L85

Added line #L85 was not covered by tests
}
defer jsonFile.Close()

_, err = jsonFile.Write(jsonData)
if err != nil {
log.Fatal(errors.New(fmt.Sprint("Error writing JSON data: ", err)))
log.Fatal(fmt.Errorf("error writing JSON data: %w", err))

Check warning on line 91 in crit/cli/cli.go

View check run for this annotation

Codecov / codecov/patch

crit/cli/cli.go#L91

Added line #L91 was not covered by tests
}
},
}
Expand All @@ -80,16 +99,43 @@
Short: "Convert JSON to binary image file",
Long: "Convert the input JSON to a CRIU image file.",
Run: func(cmd *cobra.Command, args []string) {
c = crit.NewCli(inputFilePath, outputFilePath,
var (
inputFile, outputFile *os.File
err error
)
if inputFilePath == "" {
inputFile = os.Stdin
} else {
inputFile, err = os.Open(inputFilePath)
if err != nil {
log.Fatal(fmt.Errorf("error opening input file: %w", err))
}

Check warning on line 112 in crit/cli/cli.go

View check run for this annotation

Codecov / codecov/patch

crit/cli/cli.go#L111-L112

Added lines #L111 - L112 were not covered by tests
defer inputFile.Close()
}
if outputFilePath == "" {
outputFile = os.Stdout
} else {
outputFile, err = os.Create(outputFilePath)
if err != nil {
log.Fatal(fmt.Errorf("error opening output file: %w", err))
}

Check warning on line 121 in crit/cli/cli.go

View check run for this annotation

Codecov / codecov/patch

crit/cli/cli.go#L120-L121

Added lines #L120 - L121 were not covered by tests
defer outputFile.Close()
}

c = crit.New(inputFile, outputFile,
inputDirPath, pretty, noPayload)
entryType, err := GetEntryTypeFromJSON(inputFile)
if err != nil {
log.Fatal(fmt.Errorf("error getting protobuf binding: %w", err))
}
// Convert JSON to Go struct
img, err := c.Parse()
img, err := c.Parse(entryType)
if err != nil {
log.Fatal(err)
log.Fatal(fmt.Errorf("error parsing JSON: %w", err))

Check warning on line 134 in crit/cli/cli.go

View check run for this annotation

Codecov / codecov/patch

crit/cli/cli.go#L134

Added line #L134 was not covered by tests
}
// Write Go struct to binary image file
if err := c.Encode(img); err != nil {
log.Fatal(err)
log.Fatal(fmt.Errorf("error writing to file: %w", err))

Check warning on line 138 in crit/cli/cli.go

View check run for this annotation

Codecov / codecov/patch

crit/cli/cli.go#L138

Added line #L138 was not covered by tests
}
},
}
Expand All @@ -103,16 +149,34 @@
Run: func(cmd *cobra.Command, args []string) {
inputFilePath = args[0]
pretty = true
c = crit.NewCli(inputFilePath, outputFilePath,
var (
inputFile *os.File
err error
)
if inputFilePath == "" {
inputFile = os.Stdin

Check warning on line 157 in crit/cli/cli.go

View check run for this annotation

Codecov / codecov/patch

crit/cli/cli.go#L157

Added line #L157 was not covered by tests
} else {
inputFile, err = os.Open(inputFilePath)
if err != nil {
log.Fatal(fmt.Errorf("error opening input file: %w", err))
}

Check warning on line 162 in crit/cli/cli.go

View check run for this annotation

Codecov / codecov/patch

crit/cli/cli.go#L161-L162

Added lines #L161 - L162 were not covered by tests
defer inputFile.Close()
}

c = crit.New(inputFile, nil,
inputDirPath, pretty, noPayload)
img, err := c.Decode()
entryType, err := GetEntryTypeFromImg(inputFile)
if err != nil {
log.Fatal(err)
log.Fatal(fmt.Errorf("error getting protobuf binding: %w", err))
}

Check warning on line 171 in crit/cli/cli.go

View check run for this annotation

Codecov / codecov/patch

crit/cli/cli.go#L170-L171

Added lines #L170 - L171 were not covered by tests
img, err := c.Decode(entryType)
if err != nil {
log.Fatal(fmt.Errorf("error decoding image: %w", err))

Check warning on line 174 in crit/cli/cli.go

View check run for this annotation

Codecov / codecov/patch

crit/cli/cli.go#L174

Added line #L174 was not covered by tests
}

jsonData, err := json.MarshalIndent(img, "", " ")
if err != nil {
log.Fatal(errors.New(fmt.Sprint("Error processing data into JSON: ", err)))
log.Fatal(fmt.Errorf("error processing data into JSON: %w", err))

Check warning on line 179 in crit/cli/cli.go

View check run for this annotation

Codecov / codecov/patch

crit/cli/cli.go#L179

Added line #L179 was not covered by tests
}
fmt.Println(string(jsonData))
},
Expand All @@ -125,16 +189,30 @@
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
inputFilePath = args[0]
c = crit.NewCli(inputFilePath, outputFilePath,
var (
inputFile *os.File
err error
)
if inputFilePath == "" {
inputFile = os.Stdin

Check warning on line 197 in crit/cli/cli.go

View check run for this annotation

Codecov / codecov/patch

crit/cli/cli.go#L197

Added line #L197 was not covered by tests
} else {
inputFile, err = os.Open(inputFilePath)
if err != nil {
log.Fatal(fmt.Errorf("error opening input file: %w", err))
}

Check warning on line 202 in crit/cli/cli.go

View check run for this annotation

Codecov / codecov/patch

crit/cli/cli.go#L201-L202

Added lines #L201 - L202 were not covered by tests
defer inputFile.Close()
}

c = crit.New(inputFile, nil,
inputDirPath, pretty, noPayload)
img, err := c.Info()
if err != nil {
log.Fatal(err)
log.Fatal(fmt.Errorf("error decoding image: %w", err))

Check warning on line 210 in crit/cli/cli.go

View check run for this annotation

Codecov / codecov/patch

crit/cli/cli.go#L210

Added line #L210 was not covered by tests
}

jsonData, err := json.MarshalIndent(img, "", " ")
if err != nil {
log.Fatal(errors.New(fmt.Sprint("Error processing data into JSON: ", err)))
log.Fatal(fmt.Errorf("error processing data into JSON: %w", err))

Check warning on line 215 in crit/cli/cli.go

View check run for this annotation

Codecov / codecov/patch

crit/cli/cli.go#L215

Added line #L215 was not covered by tests
}
fmt.Println(string(jsonData))
},
Expand All @@ -155,10 +233,10 @@
// returned object since we don't really care
// about the data itself, as long as we can
// marshal it into JSON and display it.
var xData interface{}
var xData any
var err error

c = crit.NewCli(inputFilePath, outputFilePath,
c = crit.New(nil, nil,
inputDirPath, pretty, noPayload)
// Switch the explore type and call the handler.
switch args[1] {
Expand All @@ -171,22 +249,22 @@
case "rss":
xData, err = c.ExploreRss()
default:
err = errors.New("error exploring directory: Invalid explore type")
err = errors.New("error exploring directory: invalid explore type")

Check warning on line 252 in crit/cli/cli.go

View check run for this annotation

Codecov / codecov/patch

crit/cli/cli.go#L252

Added line #L252 was not covered by tests
}
if err != nil {
log.Fatal(err)
log.Fatal(fmt.Errorf("error exploring directory: %w", err))

Check warning on line 255 in crit/cli/cli.go

View check run for this annotation

Codecov / codecov/patch

crit/cli/cli.go#L255

Added line #L255 was not covered by tests
}

jsonData, err := json.MarshalIndent(xData, "", " ")
if err != nil {
log.Fatal(errors.New(fmt.Sprint("Error processing data into JSON: ", err)))
log.Fatal(fmt.Errorf("error processing data into JSON: %w", err))

Check warning on line 260 in crit/cli/cli.go

View check run for this annotation

Codecov / codecov/patch

crit/cli/cli.go#L260

Added line #L260 was not covered by tests
}
fmt.Println(string(jsonData))
},
}

// Add all commands to the root command and configure flags
func init() {
func Init() {
// Disable completion generation
rootCmd.CompletionOptions.DisableDefaultCmd = true
// Decode options
Expand All @@ -212,8 +290,8 @@
rootCmd.AddCommand(xCmd)
}

func main() {
func Run() {
if err := rootCmd.Execute(); err != nil {
log.Fatal(err)
log.Fatal(fmt.Errorf("error running CLI: %w", err))

Check warning on line 295 in crit/cli/cli.go

View check run for this annotation

Codecov / codecov/patch

crit/cli/cli.go#L295

Added line #L295 was not covered by tests
}
}
Loading