diff --git a/.azure-pipelines/build-template.yml b/.azure-pipelines/build-template.yml index 794e67977..0f67b8287 100644 --- a/.azure-pipelines/build-template.yml +++ b/.azure-pipelines/build-template.yml @@ -131,6 +131,7 @@ jobs: sudo mkdir /usr/local/yang-models sudo dpkg -i libswsscommon_*.deb + sudo dpkg -i libswsscommon-dev_*.deb sudo dpkg -i python-swsscommon_*.deb ./tests/tests @@ -138,6 +139,9 @@ jobs: pytest --cov=. --cov-report=xml mv coverage.xml tests/coverage.xml gcovr -r ./ -e ".*/swsscommon_wrap.cpp" --exclude-unreachable-branches --exclude-throw-branches -x --xml-pretty -o coverage.xml + make -C goext + redis-cli FLUSHALL + make -C goext check rm -rf $(Build.ArtifactStagingDirectory)/download displayName: "Run swss common unit tests" diff --git a/common/configdb.h b/common/configdb.h index 38084f019..e6513c2ed 100644 --- a/common/configdb.h +++ b/common/configdb.h @@ -37,7 +37,7 @@ class ConfigDBConnector_Native : public SonicV2Connector_Native std::string m_db_name; }; -#ifdef SWIG +#if defined(SWIG) && defined(SWIGPYTHON) %pythoncode %{ ## Note: diamond inheritance, reusing functions in both classes class ConfigDBConnector(SonicV2Connector, ConfigDBConnector_Native): @@ -307,7 +307,7 @@ class ConfigDBPipeConnector_Native: public ConfigDBConnector_Native int _get_config(DBConnector& client, RedisTransactioner& pipe, std::map>>& data, int cursor); }; -#ifdef SWIG +#if defined(SWIG) && defined(SWIGPYTHON) %pythoncode %{ class ConfigDBPipeConnector(ConfigDBConnector, ConfigDBPipeConnector_Native): diff --git a/common/countertable.h b/common/countertable.h index 4b906732d..44d58a0e2 100644 --- a/common/countertable.h +++ b/common/countertable.h @@ -68,7 +68,7 @@ class KeyCache { return m_keyMap.at(name); } -#ifdef SWIG +#if defined(SWIG) && defined(SWIGPYTHON) %pythoncode %{ def __getitem__(self, name): return self.at(name) diff --git a/common/dbconnector.h b/common/dbconnector.h index 8a70a2cfb..42c50531f 100644 --- a/common/dbconnector.h +++ b/common/dbconnector.h @@ -41,7 +41,7 @@ class SonicDBConfig static constexpr const char *DEFAULT_SONIC_DB_CONFIG_FILE = "/var/run/redis/sonic-db/database_config.json"; static constexpr const char *DEFAULT_SONIC_DB_GLOBAL_CONFIG_FILE = "/var/run/redis/sonic-db/database_global.json"; static void initialize(const std::string &file = DEFAULT_SONIC_DB_CONFIG_FILE); -#ifdef SWIG +#if defined(SWIG) && defined(SWIGPYTHON) %pythoncode %{ ## TODO: the python function and C++ one is not on-par @staticmethod @@ -51,7 +51,7 @@ class SonicDBConfig #endif static void initializeGlobalConfig(const std::string &file = DEFAULT_SONIC_DB_GLOBAL_CONFIG_FILE); -#ifdef SWIG +#if defined(SWIG) && defined(SWIGPYTHON) %pythoncode %{ ## TODO: the python function and C++ one is not on-par @staticmethod @@ -70,7 +70,7 @@ class SonicDBConfig static std::string getDbHostname(const std::string &dbName, const std::string &netns = EMPTY_NAMESPACE); static int getDbPort(const std::string &dbName, const std::string &netns = EMPTY_NAMESPACE); static std::vector getNamespaces(); -#ifdef SWIG +#if defined(SWIG) && defined(SWIGPYTHON) %pythoncode %{ ## TODO: the python function and C++ one is not on-par @staticmethod @@ -163,7 +163,7 @@ class DBConnector : public RedisContext std::string getDbName() const; std::string getNamespace() const; -#ifdef SWIG +#if defined(SWIG) && defined(SWIGPYTHON) %pythoncode %{ namespace = property(getNamespace) %} @@ -181,7 +181,7 @@ class DBConnector : public RedisContext int64_t del(const std::string &key); -#ifdef SWIG +#if defined(SWIG) && defined(SWIGPYTHON) // SWIG interface file (.i) globally rename map C++ `del` to python `delete`, // but applications already followed the old behavior of auto renamed `_del`. // So we implemented old behavior for backward compatibility diff --git a/common/producerstatetable.h b/common/producerstatetable.h index 95061bb51..a5c4b2adc 100644 --- a/common/producerstatetable.h +++ b/common/producerstatetable.h @@ -25,7 +25,7 @@ class ProducerStateTable : public TableBase, public TableName_KeySet const std::string &op = DEL_COMMAND, const std::string &prefix = EMPTY_PREFIX); -#ifdef SWIG +#if defined(SWIG) && defined(SWIGPYTHON) // SWIG interface file (.i) globally rename map C++ `del` to python `delete`, // but applications already followed the old behavior of auto renamed `_del`. // So we implemented old behavior for backward compatibility diff --git a/common/producertable.h b/common/producertable.h index ff0c4cb75..953b3d332 100644 --- a/common/producertable.h +++ b/common/producertable.h @@ -35,7 +35,7 @@ class ProducerTable : public TableBase, public TableName_KeyValueOpQueues const std::string &op = DEL_COMMAND, const std::string &prefix = EMPTY_PREFIX); -#ifdef SWIG +#if defined(SWIG) && defined(SWIGPYTHON) // SWIG interface file (.i) globally rename map C++ `del` to python `delete`, // but applications already followed the old behavior of auto renamed `_del`. // So we implemented old behavior for backward compatibility diff --git a/common/sonicv2connector.h b/common/sonicv2connector.h index d700d926f..2bfc937ef 100644 --- a/common/sonicv2connector.h +++ b/common/sonicv2connector.h @@ -62,7 +62,7 @@ class SonicV2Connector_Native std::string m_netns; }; -#ifdef SWIG +#if defined(SWIG) && defined(SWIGPYTHON) %pythoncode %{ class SonicV2Connector(SonicV2Connector_Native): diff --git a/common/table.h b/common/table.h index 2ba765f7d..a575897db 100644 --- a/common/table.h +++ b/common/table.h @@ -146,7 +146,7 @@ class TableEntryPoppable { } }; -#ifdef SWIG +#if defined(SWIG) && defined(SWIGPYTHON) %pythoncode %{ def transpose_pops(m): return [tuple(m[j][i] for j in range(len(m))) for i in range(len(m[0]))] @@ -207,7 +207,7 @@ class Table : public TableBase, public TableEntryEnumerable { /* Get the configured ttl value for key */ bool ttl(const std::string &key, int64_t &reply_value); -#ifdef SWIG +#if defined(SWIG) && defined(SWIGPYTHON) // SWIG interface file (.i) globally rename map C++ `del` to python `delete`, // but applications already followed the old behavior of auto renamed `_del`. // So we implemented old behavior for backward compatibility diff --git a/goext/Makefile b/goext/Makefile new file mode 100644 index 000000000..7f59ca6e5 --- /dev/null +++ b/goext/Makefile @@ -0,0 +1,24 @@ +export CGO_LDFLAGS := -lswsscommon -lhiredis +export CGO_CXXFLAGS := -I../ -I../common/ -w -Wall -fpermissive + +GO ?= /usr/local/go/bin/go +SWIG ?= /usr/bin/swig +RM=rm -f +LN=ln + +SWIG_FLAG = -go -cgo -c++ -intgosize 64 +ifeq ($(CONFIGURED_ARCH),arm64) +SWIG_FLAG += -DSWIGWORDSIZE64 +endif + +.PHONY: all check clean + +all: + $(SWIG) $(SWIG_FLAG) -I/usr/include/swss/ swsscommon.i + +check: + sudo CGO_LDFLAGS="$(CGO_LDFLAGS)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" $(GO) test + +clean: + $(RM) swsscommon.go *.cxx + diff --git a/goext/swsscommon.i b/goext/swsscommon.i new file mode 120000 index 000000000..5e46e6ab5 --- /dev/null +++ b/goext/swsscommon.i @@ -0,0 +1 @@ +../pyext/swsscommon.i \ No newline at end of file diff --git a/goext/swsscommon_test.go b/goext/swsscommon_test.go new file mode 100644 index 000000000..cbefd96f8 --- /dev/null +++ b/goext/swsscommon_test.go @@ -0,0 +1,36 @@ +package swsscommon + +import ( + "testing" + "os" + "path/filepath" +) + +func TestSonicDBConfig(t *testing.T) { + pwd, _ := os.Getwd() + SonicDBConfigInitialize(filepath.Dir(pwd) + "/tests/redis_multi_db_ut_config/database_config.json") +} + +func TestProducerStateTable(t *testing.T) { + db := NewDBConnector("APPL_DB", uint(0), true) + pt := NewProducerStateTable(db, "TEST_TABLE") + tbl := NewTable(db, "_TEST_TABLE") + vec := NewFieldValuePairs() + key := "aaa" + pair := NewFieldValuePair("a", "b") + vec.Add(pair) + pt.Set(key, vec, "SET", "") + fvs := NewFieldValuePairs() + ret := tbl.Get("aaa", fvs) + if ret != true { + t.Errorf("Get table failed") + } + fv := fvs.Get(0) + if fv.GetFirst() != "a" { + t.Errorf("Wronge fv first %v", fv.GetFirst()) + } + if fv.GetSecond() != "b" { + t.Errorf("Wronge fv second: %v", fv.GetSecond()) + } +} + diff --git a/pyext/swsscommon.i b/pyext/swsscommon.i index daca2dbf3..266f296dc 100644 --- a/pyext/swsscommon.i +++ b/pyext/swsscommon.i @@ -49,7 +49,9 @@ %include %include %include +#ifdef SWIGPYTHON %include +#endif %include %include %include @@ -64,6 +66,7 @@ %template(GetConfigResult) std::map>>; %template(GetInstanceListResult) std::map; +#ifdef SWIGPYTHON %exception { try { @@ -136,6 +139,7 @@ temp = SWIG_NewPointerObj(*$1, SWIGTYPE_p_swss__Selectable, 0); SWIG_Python_AppendOutput($result, temp); } +#endif %inline %{ template